java - 如何让我的 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();
}
}
我正在尝试开始工作的循环代码 ^
解决方案
正如我在评论中提到的,将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;
}
}
}
}
推荐阅读
- php - 语法错误或访问冲突:1064 - PDO 拒绝执行多个查询(包括 CREATE TEMPORARY TABLE)
- visual-studio-code - 如何在 VSCode 扩展中添加这些代码完成功能?
- webpack - Webpack、Babel 7.4.0 和 core-js 3
- aws-pinpoint - 是否可以通过 REST API 创建 Amazon Pinpoint 获利事件?
- javascript - Date.getDate() 函数是如何工作的?
- c# - 如何在 ilNumerics 中使用 ZXYPositions 正确绘制 3D 表面?
- r - 如何调用R6父类中的函数,其中父函数依赖于其他重写的辅助函数
- python - 如何将 HTML 表中的数据抓取到 Python 列表/字典中?
- barcode - Acumatica 和 code128 条码无法扫描
- android-studio - 如何在 Android Studio 上摆脱这个鼠标指针并获得可编辑的指针