import java.util.Scanner;
//求输入的整数的正负个数及他们的和与平均值
public class CountPositivesAndNegetivesThenComputeTotalAndAverage {
public static void main(String[] args) {
int sum1 = 0, sum2 = 0, time = 0, number = 1, total = 0;
double average = 0;
System.out.println("Ennter an int value,the program exits if the input is 0: ");
while (number != 0) {
Scanner input = new Scanner(System.in);
number = input.nextInt();
time = time + 1;
total = number + total;
average = (double) total / time;
if (number > 0) {
sum1 = sum1 + 1;
}
if (number < 0) {
sum2 = sum2 + 1;
}
}
System.out.println("The number of positives is " + sum1);
System.out.println("The number of negetives is " + sum2);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
}
}
------解决方案--------------------
将 Scanner input = new Scanner(System.in);
移到循环外,即while (number != 0) {
的前面