2014-11-03 97 views
0

我在这里有点迷路。我有一个for循环增加值,是这样的:Java for循环(数字总数+总数)

int nu01 = 10000; 
int level = 0; 
int increase = 35000; 

for (int i = 0; i < 100; i++) { 
    nu01 = nu01 + increase; 
    level++; 
    System.out.println(nu01); 

    if (level > 50) { 
     increase = 45000; 
    } 

这工作得很好,但我需要的所有号码的总和,从环占总:

 
loop: 10,20,30,40,50,70,90,120.... 
total:10,30,60,100,150,220,310,430... 

我想:

int total; 
total=nu01 + nu01; //or nu01 + nu01 + increase; 

但我得到奇怪的总和。所以我需要循环增加所有这些数字的数字和总和。我希望这是有道理的。谢谢。

回答

2

你想要做的是沿着

int total = 0; 
... 
//beginning of your for loop 
total = total + nu01; // alternatively you could do total += nu01; 
+0

tnx很多这个工作。 – user2722931 2014-11-04 16:42:54

0

我可能会在这里误会你行的东西,但我相信你想是这样的

public class forLoops{ 

    public static void main(String args[]){ 

     //Initialisation 
     int level = 0; 
     int increase = 35000; 
     int total = 10000; 

     //For Loop 
     for(int i = 0; i < 100; i++){ 

      total += increase; //Same as total = total + increase; 
      level++; 
      System.out.println(total); 

     if(level >= 50){ 

      increase = 45000; 

     } 
     } 
    } 
} 
0

共申报变量然后存储tota int total = 0;

total + = nu01;