You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Do-While循环问题:如何实现用户继续(y/n)提示功能?

Fixing Your Java Grade Loop Issue

Hey Edwin, let's sort out that annoying loop repetition problem you're seeing! The root causes are a mix of redundant Scanner instances and messy input handling that's throwing off your loop logic. Let's break down what's wrong and how to fix it:

Key Problems in Your Code

  • Multiple Scanner instances: You created three separate Scanner objects (kb, input, inputScanner) all reading from System.in. This is unnecessary and causes conflicts because each Scanner tries to manage the same input stream, leading to unexpected reads.
  • Unconsumed newline after nextInt(): When you use nextInt() to read the serial number and score, it leaves the newline character (from the user pressing Enter) in the input stream. The subsequent nextLine() picks up this empty newline instead of the user's actual y/n input, which messes up your loop check.
  • Double input read in loop condition: Your do-while loop first reads input into userInput, then calls input.next() again in the condition. This means you're reading the user's choice twice per loop, leading to the "repeat execution" bug.

Corrected Code

public static void main(String[] args) {
    // Use ONLY ONE Scanner for all input
    Scanner scanner = new Scanner(System.in);
    String grade = "";
    String userInput = "";

    do {
        System.out.print("Enter your serial no: ");
        int serial = scanner.nextInt();
        
        System.out.print("Enter Score: ");
        int score = scanner.nextInt();
        
        // Consume the leftover newline after nextInt()
        scanner.nextLine();

        // Grade determination logic (unchanged, works fine!)
        if (score < 0 || score > 100) {
            System.out.println("Invalid Score");
        } else if (score > 84) {
            grade = "A";
            System.out.println("Your grade is: " + grade);
        } else if (score > 74) {
            grade = "B";
            System.out.println("Your grade is: " + grade);
        } else if (score > 64) {
            grade = "C";
            System.out.println("Your grade is: " + grade);
        } else if (score > 49) {
            grade = "D";
            System.out.println("Your grade is: " + grade);
        } else {
            grade = "F";
            System.out.println("Your grade is: " + grade);
        }

        // Ask for continuation and read the input once
        System.out.println("Do you want to continue y/n");
        userInput = scanner.nextLine();

    // Check the stored userInput to decide if we loop again
    } while (userInput.equalsIgnoreCase("y"));

    scanner.close();
}

What Changed?

  1. Single Scanner Instance: We now use one Scanner for all input operations, eliminating stream conflicts.
  2. Newline Consumption: After each nextInt(), we call scanner.nextLine() to clear the leftover newline character, so the subsequent nextLine() reads the user's actual y/n input correctly.
  3. Single Input Read for Loop Check: We read the user's choice once into userInput, then use that variable in the do-while condition. No more double-reading input, which fixes the repetition bug.

Test this out—you'll notice the loop now behaves exactly as expected: it runs once, asks if you want to continue, and only repeats if you enter "y" (case-insensitive).

内容的提问来源于stack exchange,提问作者Edwin Kong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 15:32:41