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
Scannerobjects (kb,input,inputScanner) all reading fromSystem.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 usenextInt()to read the serial number and score, it leaves the newline character (from the user pressing Enter) in the input stream. The subsequentnextLine()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 callsinput.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?
- Single Scanner Instance: We now use one
Scannerfor all input operations, eliminating stream conflicts. - Newline Consumption: After each
nextInt(), we callscanner.nextLine()to clear the leftover newline character, so the subsequentnextLine()reads the user's actual y/n input correctly. - 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
相关产品推荐
相关产品推荐

