【问题标题】:Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info? [duplicate]Java String Scanner 输入不等待信息,直接移动到下一条语句。如何等待信息? [复制]
【发布时间】:2011-12-14 05:16:46
【问题描述】:

我正在编写一个简单的程序,提示用户输入学生人数,然后要求用户输入每个学生的姓名和分数,以确定哪个学生的分数最高。

我已经编写了程序代码并且可以编译。第一行询问一些学生并等待输入。第二行应该询问学生姓名并等待输入,然后第三行应该打印 ans ask for that student's score, and waiting for input 但在第二行打印后,第三行立即被调用(第二行确实不等待输入),然后在第三行之后尝试输入请求的信息时出现运行时错误。

如何调整代码,以便在打印第三行之前打印第二行并等待输入字符串?

import java.util.Scanner;

public class HighestScore {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of students: ");
        int numOfStudents = input.nextInt();

        System.out.print("Enter a student's name: ");
        String student1 = input.nextLine();

        System.out.print("Enter that student's score: ");
        int score1 = input.nextInt();

        for (int i = 0; i <= numOfStudents - 1; i++) {

            System.out.println("Enter a student's name: ");
            String student = input.nextLine();

            System.out.println("Enter that student's score: ");
            int score = input.nextInt();

            if (score > score1) {
            student1 = student;
            score1 = score;
            }
        }
        System.out.println("Top student " +
        student1 + "'s score is " + score1);
    }
}

【问题讨论】:

    标签: java string java.util.scanner


    【解决方案1】:

    这就是为什么我不喜欢使用Scanner,因为这种行为。 (一旦我了解了发生的事情并对此感到满意,我就会非常喜欢 Scanner。

    发生的情况是对nextLine() 的调用首先完成了用户输入学生人数的那一行。为什么?因为nextInt() 只读取了一个 int 并没有完成这一行。

    所以添加一个额外的readLine() 语句可以解决这个问题。

    System.out.print("Enter the number of students: ");
    int numOfStudents = input.nextInt();
    
    // Skip the newline
    input.nextLine();
    
    System.out.print("Enter a student's name: ");
    String student1 = input.nextLine();
    

    正如我已经提到的,我不喜欢使用 Scanner。我以前做的是使用 BufferedReader。这是更多的工作,但实际发生的事情要简单得多。您的应用程序将如下所示:

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println("Enter the number of students: ");
    int numOfStudents = Integer.parseInt(input.readLine());
    
    String topStudent = null;
    int topScore = 0;
    for (int i = 0; i < numOfStudents; ++i)
    {
        System.out.print("Enter the name of student " + (i + 1) + ": ");
        String student = input.nextLine();
    
        // Check if this student did better than the previous top student
        if (score > topScore)
        {
             topScore = score;
             topStudent = student;
        }
    }
    

    【讨论】:

    • 这很有道理....我正在使用扫描仪并感到恼火
    【解决方案2】:

    哇,这看起来像糟糕的设计 - 如果您要求一个整数并执行 nextInteger() 扫描仪会给您整数,但它现在在其缓冲区中保存一个换行符,因为用户必须按 enter提交整数,所以如果你以后想提示用户输入一个字符串,它不会等待输入,只是给你一个换行符。 您无法清除扫描仪以避免此类问题...

    我错过了什么?

    亚当

    【讨论】:

    • 你不是说nextInt()吗?
    【解决方案3】:
        System.out.print("Enter the number of students: ");
        int numOfStudents = input.nextInt();
        // Eat the new line
        input.nextLine();
        System.out.print("Enter a student's name: ");
        String student1 = input.nextLine();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 2014-06-20
      • 2021-01-31
      • 2016-10-28
      • 2019-08-16
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多