【发布时间】:2013-11-18 08:54:22
【问题描述】:
我需要有关如何计算 while 循环打印的数字总和的帮助。我必须使用 while 循环获得数字 1 到 100 并一起计算所有这些数字。比如 1+2+3...+98+99+100。我可以得到数字,但不能一起计算它们。这是我的代码:
public class Loops {
public static void main(String[] args) throws Exception {
int i = 1;
while (i < 101) {
System.out.print(i);
i = i + 1;
}
}
}
如何让它只打印最后一个总和?如果我试图欺骗方程式,它就会挂起。
【问题讨论】:
-
您是在问如何声明另一个变量并为其添加
i的值?
标签: java while-loop