Java String Scanner 输入不等待信息,直接移至下一条语句。如何等待信息?

发布于 2024-12-11 08:45:36 字数 1210 浏览 0 评论 0原文

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

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

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

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

I am writing a simple program that prompts a user to enter a number of students, then asks the user to enter each student's name and score in order to determine which student has the highest score.

I have written the program code and it compiles. First line asks for a number of students and waits for input. The second line is supposed to ask for a student name and wait for input, then a third line should print ans ask for that student's score, and wait for input but after the second line prints, the third line is immediately called (2nd line does not wait for input) and then I get a runtime error when trying to enter the requested information after the third line.

How do I adjust the code so that the second line prints and waits for a string to be entered before printing the third line?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一花一树开 2024-12-18 08:45:36

这就是为什么我不喜欢使用扫描仪,因为这种行为。 (一旦我了解了正在发生的事情并对此感到满意,我就非常喜欢 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();

正如我已经提到的,我不喜欢使用扫描仪。我以前做的是使用 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;
    }
}

That's why I don't didn't like using a Scanner, because of this behavior. (Once I understood what was happening, and felt comfortable with it, I like Scanner a lot).

What is happening is that the call to nextLine() first finishes the line where the user enters the number of students. Why? Because nextInt() reads only one int and does not finish the line.

So adding an extra readLine() statement would solve this problem.

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

As I already mentioned, I didn't like using Scanner. What I used to do was to use a BufferedReader. It's more work, but it's slightly more straightforward what is actually happening. Your application would look like this:

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;
    }
}
早乙女 2024-12-18 08:45:36
    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();
    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();
这样的小城市 2024-12-18 08:45:36

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

我错过了什么?

亚当

Wow that looks like bad design - if you ask for an integer an d do a nextInteger() the scanner will give you the integer, but it is now holding a new line character in its buffer as the user has to press enter to submit the integer, so if you later want to prompt the user for a string it will not wait for input and just give you back a new line character.
You cant clear the scanner to avoid this sort of problem...

What am I missing?

Adam

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文