我想获得 int[] 数组存储的 int 值的总和。代码仍然有错误。是否有任何代码可以使这行代码看起来简化?我想制作没有数据库的 52 周金钱挑战应用程序。
我有这个公式:iA + sum = total[] + aA = tA;
我有一个错误,前一个值没有被前一个值添加。
附注。周、iA(初始金额)、aA(添加金额)、sum、sT 均为 int。tA 现在被添加到总数 [] 中。
PSS:对于标记这个问题的每个人都有重复的问题,我已经看到了那个问题,它没有回答我的问题。该帖子具有由程序员/编码员创建的自己的值,我询问如何将前一个元素添加到两个相加整数值的值中。我已经尝试过该问题中给出的解决方案,但它不适用于我的代码。它给出了不同的值(不需要的值)。
int[] total = new int[weeks + 1];
int sum = 0;
int tA = 0;
int ai = 0;
for(int sT = 1 ; sT <= weeks; sT++){ // error
sum += iA;
System.out.println("The payable for the week " + sT + " is: " + sum);
total[sT] = sum;
tA = total[sT];
if(sT == 1){
System.out.println("if: " + tA);
}
else if(sT == 2){
tA += iA;
System.out.println("else if: " + tA);
}
else{
tA += aA;
System.out.println("else: " + tA);
}
}
System.out.println(Arrays.toString(total)); //print stored values of array
这个问题已经有了答案。
for(int sT = 1 ; sT <= weeks; sT++){
sum += iA;
System.out.println("The payable for the week " + sT + " is: " + sum);
tA = sum;
if(sT == 1){
System.out.println("if: " + tA);
total[sT] = tA;
}
else if(sT == 2){
tA = iA + sum;
System.out.println("else if: " + tA);
total[sT] = tA;
}
else{
tA = sum + total[sT - 1];
System.out.println("else: " + tA);
total[sT] = tA;
}
}
System.out.println(Arrays.toString(total)); //print stored values of array
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句