2

我正在尝试自学 Java 编程,偶然发现了斯坦福提供的 CS106A 课程。这是一个很棒的免费在线课程。我看了几个讲座视频,到目前为止我很喜欢。我现在正在尝试做作业,但我遇到了我自己无法解决的问题。

这是本次作业的第 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);
    }
}
4

2 回答 2

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);
}

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


更新 : -

我会do-while用 a替换你的,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");
        }
    }
}
于 2012-11-17T10:31:26.533 回答
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...

顺便说一句 - 可以在 while 循环内计算最大值和最小值,而无需先将值存储在列表中。

感谢您提醒我注意这个免费的在线课程。

于 2012-11-17T10:35:19.177 回答