2014-11-03 101 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;