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;
但我得到了奇怪的数额。所以我需要循环来增加所有这些数字的数量和总和。我希望这是有道理的。感谢。
答案 0 :(得分:2)
你想要做的是
int total = 0;
...
//beginning of your for loop
total = total + nu01; // alternatively you could do total += nu01;
答案 1 :(得分: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;
}
}
}
}
答案 2 :(得分:0)
声明一个total变量然后存储tota int total = 0;
总计+ = nu01;