这段代码有什么问题?得到 0 作为答案
What is wrong with this code ? getting 0 as an answer
public class Sum {
public static void main(String[]args){
int sumOfPos = 0;
int sumOfNeg = 0;
int sum = sumOfPos + sumOfNeg;
//should give 1 , 5 , 9
for(int pos = 1 ; pos<= 9; pos+=4){
int Old = pos*pos;
sumOfPos = pos + Old;
}
//should give -3 and -7
for(int neg = -3 ; neg>= -7; neg-=4){
int Old = neg*neg;
sumOfNeg = neg + Old;
}
// should give the sum of the sqaure of these numbers
System.out.println(sum);
}
我得到零是什么问题?
我只能为这个使用循环。
获得所需结果的其他方法是什么?
public class Sum {
public static void main(String[]args){
int sumOfPos = 0;
int sumOfNeg = 0;
//should give 1 , 5 , 9
for(int pos = 1 ; pos<= 9; pos+=4){
int Old = pos*pos;
sumOfPos = pos + Old;
}
//should give -3 and -7
for(int neg = -3 ; neg>= -7; neg-=4){
int Old = neg*neg;
sumOfNeg = neg + Old;
}
// should give the sum of the sqaure of these numbers
int sum = sumOfPos + sumOfNeg;
System.out.println(sum);
}
public class Sum {
public static void main(String[]args){
int sumOfPos = 0;
int sumOfNeg = 0;
int sum = sumOfPos + sumOfNeg;
//should give 1 , 5 , 9
for(int pos = 1 ; pos<= 9; pos+=4){
int Old = pos*pos;
sumOfPos = pos + Old;
}
//should give -3 and -7
for(int neg = -3 ; neg>= -7; neg-=4){
int Old = neg*neg;
sumOfNeg = neg + Old;
}
// should give the sum of the sqaure of these numbers
System.out.println(sum);
}
我得到零是什么问题? 我只能为这个使用循环。 获得所需结果的其他方法是什么?
public class Sum {
public static void main(String[]args){
int sumOfPos = 0;
int sumOfNeg = 0;
//should give 1 , 5 , 9
for(int pos = 1 ; pos<= 9; pos+=4){
int Old = pos*pos;
sumOfPos = pos + Old;
}
//should give -3 and -7
for(int neg = -3 ; neg>= -7; neg-=4){
int Old = neg*neg;
sumOfNeg = neg + Old;
}
// should give the sum of the sqaure of these numbers
int sum = sumOfPos + sumOfNeg;
System.out.println(sum);
}