gpt4 book ai didi

java - 我怎样才能让我的While语句正确循环和中断,同时在java中输入另一个选项?

转载 作者:行者123 更新时间:2023-12-01 20:05:25 24 4
gpt4 key购买 nike

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

初级();、中级();和困难();有什么可以打电话的吗?

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

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

}
}

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

最佳答案

正如我在评论中提到的,将 break 语句移至 if 主体中,并且每次迭代仅提示一次 Difficulty 。此外,您的变量名称也偏离了 Java 标准命名约定。 难度应该是难度,并且常量应该全部大写(并且您可以使用您定义的常量)。就像,

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

关于java - 我怎样才能让我的While语句正确循环和中断,同时在java中输入另一个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58986523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com