日期:2014-05-20  浏览次数:20705 次

java nettbeans 编程问题,输入时以空格区分两个数字结果不正确,以enter则结果正确。困惑中啊,另外有什么写得不好的地方也请指出,谢谢啊
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) {
的前面