我正在尝试一个程序,它读取未指定数量的整数,找到总和、正数、负数和平均值。我的问题是它要么只运行并允许输入一个整数然后什么都不做,要么使用下面的代码,它永远不会停止让你输入数字,因此我无法通过。我的 number = 0 输出正确。
public class Compute {
// Count positive and negative numbers and compute the average of numbers
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
positive = 0;
negative = 0;
total = 0;
System.out.println("Enter an integer, the input ends if it is 0: ");
int numbers = input.nextInt();
do {
if (numbers > 0) {
positive++;//add 1 to positive count
} else if (numbers < 0) {
negative++;//add 1 to negative count
}//end else if
sum += numbers; //add integer input to sum
numbers = input.nextInt();
total++;
} while (numbers != 0);
if (numbers == 0) {
System.out.println("No numbers are entered except " + numbers);
}//end if
}
}