java编程题使用for循环计算1-2+3-4+...-100

2024-11-09 09:52:14
推荐回答(1个)
回答1:

int sum = 0,k=-1;
for(int i = 1; i <=100; i++){
k = -k;
sum = sum + (k*i);
}
System.out.println(sum);