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

Java:在PlayerOne类中实现带用户输入的while循环以完成玩家职业选择

Fixing Your Profession Selection Loop in PlayerOne Class

Hey Marko, let's get that profession selection loop working properly. I spotted a couple of straightforward issues in your code that are messing up the logic—let's break them down and fix them step by step.

First, let's call out the obvious problems in your current code:

  • The case numbers don't match your on-screen prompts: You tell users to press 1 for Knight, but your code uses case 0 for that choice. Same mismatch applies to the other professions.
  • You have duplicate case labels (case 1 appears twice), which will cause a compilation error in Java—you can't have two cases with the same value.
  • Right now, your loop runs forever even after a valid selection, since there's no logic to exit the while(true) loop once a profession is chosen.
  • You're not handling invalid inputs (like a user typing a letter instead of a number, or a number outside 1-3), which would crash or break your loop unexpectedly.

Here's the fixed version of your code, with explanations:

import java.util.Scanner;

public class PlayerOne {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean professionSelected = false; // Flag to control loop exit

        while (!professionSelected) {
            System.out.println("Choose your profession:");
            System.out.println("Press 1 for a Knight class");
            System.out.println("Press 2 for a Rider class");
            System.out.println("Press 3 for a Mage class");

            // Handle non-integer inputs to avoid crashes
            if (scanner.hasNextInt()) {
                int choice = scanner.nextInt();
                scanner.nextLine(); // Clear the remaining newline from input buffer

                switch (choice) {
                    case 1:
                        System.out.println("You've selected the Knight class!");
                        // Add your Knight initialization logic here (e.g., set player profession)
                        professionSelected = true; // Exit loop after valid selection
                        break;
                    case 2:
                        System.out.println("You've selected the Rider class!");
                        // Add your Rider initialization logic here
                        professionSelected = true;
                        break;
                    case 3:
                        System.out.println("You've selected the Mage class!");
                        // Add your Mage initialization logic here
                        professionSelected = true;
                        break;
                    default:
                        System.out.println("Invalid choice! Please enter a number between 1 and 3.\n");
                }
            } else {
                // Handle non-numeric input
                System.out.println("Oops, that's not a valid number. Please try again.\n");
                scanner.nextLine(); // Clear the invalid input from buffer
            }
        }

        scanner.close(); // Don't forget to close the scanner when done
    }
}

Key improvements made:

  • Aligned case numbers with prompts: case 1 now corresponds to Knight, case 2 to Rider, and case 3 to Mage—matching exactly what you tell users to input.
  • Removed duplicate cases: Each profession has a unique, correct case label.
  • Added loop exit control: The professionSelected boolean flag lets the loop terminate once a valid choice is made.
  • Handled invalid inputs: Checks if the input is an integer first, and uses a default case to catch numbers outside 1-3. This prevents crashes from bad user input.
  • Cleaned up input buffer: Properly clears the scanner buffer after each input to avoid unexpected behavior with subsequent inputs.

Quick tips for next steps:

  • Replace the print statements in each case with your actual profession initialization code (e.g., setting a profession field in your PlayerOne class to "Knight", etc.).
  • If you want to let users go back or change their choice, you could add an option like "Press 4 to reselect" and adjust the loop logic accordingly.

内容的提问来源于stack exchange,提问作者Marko Lubar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 12:18:12