import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int data = 0;
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
System.out.print("Enter int value (0 for exit):");
do {
data = input.nextInt();
if (data > 0)
positive += 1;
else
negative += 1;
sum += data;
count += 1;
}while (input.hasNextInt() && data != 0);
double average = sum / count;
System.out.println("The totla is " + sum);
System.out.println("The average is " + average);
}
}
執行的時候不顯示結果,0退出不了。怎樣能夠達到輸出這樣的結果啊?
將while循環裡面的條件交換一下位置就行了
while(data!=0 && input.hasNextInt())