【问题标题】:User input only occurs once or never stops during loop用户输入仅发生一次或在循环期间永不停止
【发布时间】:2013-02-08 03:47:23
【问题描述】:

我正在尝试一个程序,它读取未指定数量的整数,找到总和、正数、负数和平均值。我的问题是它要么只运行并允许输入一个整数然后什么都不做,要么使用下面的代码,它永远不会停止让你输入数字,因此我无法通过。我的 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
    }
}

【问题讨论】:

  • 请更好地格式化您的代码。如果人们可以轻松阅读您的代码,您将获得更好的答案。
  • 另外,考虑在每次调用 input.nextInt() 之前向用户输入输出提示。否则,你怎么知道是时候输入新的输入了?类似于:System.out.print("Please enter the next number: "); 后跟 numbers = input.nextIne();
  • 在再次调用nextInt之前需要消耗行终止符。
  • @Perception:通常是这样,尤其是在将 Scanner 调用与nextInt() 和类似方法与nextLine() 调用混合时,但如果 Scanner 只获得简单的标记,例如 int,那么我不'认为这是不必要的。
  • @HovercraftFullOfEels - 测试并确认。这主要是显示问题。

标签: java while-loop do-while


【解决方案1】:

试试下面的代码。在执行的任何时候终止循环并查看输出类型 0 作为输入。

import java.util.Scanner;

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;
        int positive = 0;
        int negative = 0;
        int 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
                sum += numbers; // add integer input to sum
            }

            else if (numbers < 0) {
                negative++;// add 1 to negative count
                sum -= numbers; // add integer input to sum
            }

            numbers = input.nextInt();
            total++;

        } while (numbers != 0);

        System.out.println("The number of positives is \t " + positive);
        System.out.println("The number of negatives is \t " + negative);
        System.out.println("The total count of number is \t " + total);
        System.out.println("The sum of all number is    \t" + sum);
        System.out.println("The average is           \t"
                + ((double) sum / (positive + negative)));

    }// end main
}// end Compute

【讨论】:

    【解决方案2】:

    下面的代码 sn-p 应该给你一个关于如何从控制台读取整数的好例子:

    Scanner scanner = new Scanner(System.in);
    do {
      int i = scanner.nextInt();
      // ...
    } while (scanner.hasNext());
    

    scanner.hasNext() 方法调用将阻塞,直到用户在控制台输入下一个数字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-02
      • 2020-01-14
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多