首页 > 解决方案 > 如何让我的 While 语句正确循环和中断,同时在 java 中输入另一个选项?

问题描述

我在让我的代码使用 while 语句循环时遇到问题。我希望它就像你在难度中输入 1 时一样,你会得到初学者。然后它会问你是否想要初学者。如果不是,那么你按 2。然后它会回到循环并询问你想要什么难度。然后最终打破循环。

Beginner();、Intermediate(); 和 Hard(); 有没有打电话的东西。

也可以用字符串代替选项吗?例如。

System.out.print("\nPlease choose your difficulty (1.Beginner, 2.Intermediate, 3.Hard) ");
Difficulty = option.next();

我最初希望它是你可以输入“Hard”的地方,它会问“你选择了hard。这是正确的吗?”

public static void gameSetting() {
        String settings = null;
        int Difficulty = 0;
        int Beginner = 1;
        int Intermediate = 2;
        int Hard = 3;
        System.out.print("\nPlease choose your difficulty (1.Beginner, 2.Intermediate, 3.Hard) ");
        Difficulty = option.nextInt();
        while(true){if(Difficulty==1) {
            System.out.print("You have chosen Beginner. Is this correct?");
            System.out.println("\n1.Yes, 2.No");
            int choice = option.nextInt();
            if (choice == 1) {
                Beginner();
            }
            break;
        }
        if(Difficulty == 2) {
                System.out.print("\n\nYou have chosen Intermediate. Is this correct?");
                System.out.println("\n\n1.Yes, 2.No");
                int choice = option.nextInt();
                if(choice==1) {
                    Intermediate();
                }
                break;

            }
        System.out.print("\nPlease choose your difficulty (1.Beginner, 2.Intermediate, 3.Hard) ");
        Difficulty = option.nextInt();
        if(Difficulty == 3) {
            System.out.print("\n\nYou have chosen Hard. Is this correct?");
            System.out.println("\n\n1.Yes, 2.No");
            int choice = option.nextInt();
            if(choice==1) {
                Hard();
            }
            break;
        }
        System.out.print("\nPlease choose your difficulty (1.Beginner, 2.Intermediate, 3.Hard) ");
        Difficulty = option.nextInt();

}
    }

我正在尝试开始工作的循环代码 ^

标签: javaloopswhile-loop

解决方案


正如我在评论中提到的,将break语句移动到您的正文中,并且每次迭代if只提示一次。Difficulty此外,您的变量名称偏离 Java 标准命名约定。Difficulty应该是difficulty,并且常量应该全部大写(并且您可以使用您定义的常量)。喜欢,

public static void gameSetting() {
    final int BEGINNER = 1, INTERMEDIATE = 2, HARD = 3;
    while (true) {
        System.out.println("Please choose your difficulty (1.Beginner,"
                    + " 2.Intermediate, 3.Hard) ");
        int difficulty = option.nextInt();
        if (difficulty == BEGINNER) {
            System.out.print("You have chosen Beginner. Is this correct?");
            System.out.println("\n1.Yes, 2.No");
            int choice = option.nextInt();
            if (choice == 1) {
                Beginner();
                break;
            }
        } else if (difficulty == INTERMEDIATE) {
            System.out.print("\n\nYou have chosen Intermediate. Is this correct?");
            System.out.println("\n\n1.Yes, 2.No");
            int choice = option.nextInt();
            if (choice == 1) {
                Intermediate();
                break;
            }
        } else if (difficulty == HARD) {
            System.out.print("\n\nYou have chosen Hard. Is this correct?");
            System.out.println("\n\n1.Yes, 2.No");
            int choice = option.nextInt();
            if (choice == 1) {
                Hard();
                break;
            }
        }
    }
}

此外,您可以将常见的确认消息重构为另一种方法。喜欢,

private static boolean confirmDifficulty(String difficulty) {
    System.out.printf("You have chosen %s. Is this correct?%n", difficulty);
    System.out.println("1.Yes, 2.No");
    return option.nextInt() == 1;
}

然后你的代码可能看起来像

public static void gameSetting() {
    final int BEGINNER = 1, INTERMEDIATE = 2, HARD = 3;
    while (true) {
        System.out.println("Please choose your difficulty (1.Beginner,"
                    + " 2.Intermediate, 3.Hard) ");
        int difficulty = option.nextInt();
        if (difficulty == BEGINNER) {
            if (confirmDifficulty("Beginner")) {
                Beginner();
                break;
            }
        } else if (difficulty == INTERMEDIATE) {
            if (confirmDifficulty("Intermediate")) {
                Intermediate();
                break;
            }
        } else if (difficulty == HARD) {
            if (confirmDifficulty("Hard")) {
                Hard();
                break;
            }
        }
    }
}

推荐阅读