Java:使用带有do-while循环的hasNextInt,它会偶尔忽略整数输入

时间:2012-11-17 10:25:17

标签: java list add java.util.scanner do-while

我正在尝试自己学习Java编程,并且遇到了斯坦福大学提供的CS106A课程。这是一个很棒的免费在线课程。我观看了几个lecture videos,到目前为止我很喜欢它们。我现在正在尝试进行任务,我遇到了这个问题,我自己无法解决。

这是this assignment的数字5。基本上,它需要学习者创建一个控制台程序,以获得用户输入的一些整数,并作为响应,显示最大和最小的数字。

以下代码是我所做的,问题是当我尝试输入整数时,它会跳过偶数个输入。例如,如果我输入3,12,6,15,9到控制台,它将只获得3,6,9,忽略12,15。

我做错了什么?任何帮助将不胜感激。

import java.util.*;

public class Ass2_5MaxMinNumbers {
    public static void main (String args[]) {
        Scanner scanner;

        System.out.println ("This programme finds the largest and smallest numbers.");
        System.out.println ("After finished entering, enter \"End\" (without double quotes) to show the results.");

        List<Integer> list = new ArrayList<Integer>();
        int max = 0, min = 0;

        do {
            scanner = new Scanner(System.in);

            if(scanner.hasNextInt()){
                int x = scanner.nextInt();
                list.add(x);
                System.out.println("user input: " + x);
            } else if(!scanner.hasNext("End")){
                System.out.println("Please enter an integer!");
            }
        } while (!scanner.hasNext("End"));

        max = list.get(0);
        min = list.get(0);
        for(int x = 1; x < list.size(); x++){
            if(list.get(x) > max){
                max = list.get(x);
            } else if(list.get(x) < min){
                min = list.get(x);
            }
        }

        System.out.println ("Smallest number: " + min);
        System.out.println ("Biggest number: " + max);
    }
}

2 个答案:

答案 0 :(得分:3)

Scanner.nextInt方法仅从用户传递的输入中读取下一个标记。因此,它忽略了每个输入末尾的linefeed。换行将作为下一次调用Scanner.nextInt的输入,因此您的输入将被忽略。

每次拨打Scanner.nextLine后,您都可以使用空白Scanner.nextInt来使用换行符。

if(scanner.hasNextInt()){
    int x = scanner.nextInt();
    scanner.nextLine();  // Add a blank nextLine
    list.add(x);
    System.out.println("user input: " + x);
}

或者您也可以仅使用scanner.nextLine()来阅读integers,并使用integer将输入转换为Integer.parseInt

if(scanner.hasNextInt()) {
    int x = 0;

    try {
         x = Integer.parseInt(scanner.nextLine());
    } catch (NumberFormatException e) {
         e.printStackTrace();
    }

    list.add(x);
    System.out.println("user input: " + x);
}

实际上,由于您在if中使用scanner.hasNextInt,因此您try-catch周围并不需要Integer.parseInt,因此您可以将其删除。


更新: -

我会将do-while替换为while,然后从内部删除if-else支票。

while (scanner.hasNextLine()) {
    String input = scanner.nextLine();

    if (input.equals("End")) {
        break;
    } else {
        try {
            int num = Integer.parseInt(input);
            list.add(num);

        } catch (NumberFormatException e) {
            System.out.println("Please Enter an Integer");
        }
    }
}

答案 1 :(得分:2)

您发布的代码中的错误是您正在创建一个新的Scanner对象,并且迭代循环。在循环和代码工作之前初始化扫描程序:

    scanner = new Scanner(System.in);
    do {
      if(scanner.hasNextInt()){
            int x = scanner.nextInt();
            list.add(x);
            System.out.println("user input: " + x);
        }
        //and so on...

BTW - 可以在while循环内计算最大值和最小值,而无需先将值存储在List中。

感谢您关注此免费在线课程。