Java for循环(数字总和+;总数)
我有点迷路了。我有一个Java for循环(数字总和+;总数),java,loops,for-loop,sum,Java,Loops,For Loop,Sum,我有点迷路了。我有一个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; }
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;
}
但我得到了奇怪的数字。所以我需要一个循环,它增加数字,和所有这些数字的和。我希望这是有道理的。谢谢。你想做的事与
int total = 0;
...
//beginning of your for loop
total = total + nu01; // alternatively you could do total += nu01;
我可能误解了你,但我相信你想要这样的东西
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;
}
}
}
}
forLoops的公共类{
公共静态void main(字符串参数[]){
//初始化
智力水平=0;
整数增加=35000;
总数=10000;
//For循环
对于(int i=0;i<100;i++){
合计+=增加;//与合计=合计+增加相同;
级别++;
系统输出打印项次(总计);
如果(级别>=50){
增加=45000;
}
}
}
}
声明一个总变量,然后存储tota
int-total=0
总数+=nu01
int total = 0;
...
//beginning of your for loop
total = total + nu01; // alternatively you could do total += nu01;
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;
}
}
}
}