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退出不了。怎样能够达到输出这样的结果啊?
- Enter int value(0 for exit):
- 1 2 -1 3 0 (enter)
- The number of positives is 3
- The number of positives is 1
- The number of positives is 1The total is 5
- The average is 1.25