【发布时间】:2013-08-05 00:02:06
【问题描述】:
我正在尝试读取并添加(仅)正整数,直到输入负整数。该程序应该仅在输入负整数时停止。我能想到的最好的代码是下面的代码,但问题是即使读取负整数它也不会停止。 PS:我还在学习中,请多多包涵。我尝试将 && input2!<0 添加到 while 循环条件中,但它作为一个错误出现。关于如何让它按照我想要的方式运行的任何建议?谢谢。
public class Total{
public static void main(String[] args){
System.out.println("Enter an integer: ");
Scanner entry = new Scanner(System.in);
int input1 = entry.nextInt();
System.out.println("Enter another integer: ");
int input2 = entry.nextInt();
int total = input1 + input2;
while (input2 > 0){
System.out.println(total + "\nEnter another interger: ");
total += entry.nextInt();
}
}
}
【问题讨论】:
标签: java loops while-loop