当用户输入 "no" 时,我如何让 while 循环终止
How would I get a while loop to terminate when the user inputs "no"
我有一个 21 点的 while 循环 Class:
while (playerSum < 21) {
System.out.println("Would you like another card?");
String input;
System.out.flush();
input = in.readLine();
if (input.equalsIgnoreCase("yes")){
int card3;
Random r3 = new Random();
card3 = r3.nextInt(11 - 2 + 1) + 2;
switch (card3) {
case 2: System.out.println("You were dealt a Two"); break;
case 3: System.out.println("You were dealt a Three"); break;
case 4: System.out.println("You were dealt a Four"); break;
case 5: System.out.println("You were dealt a Five"); break;
case 6: System.out.println("You were dealt a Six"); break;
case 7: System.out.println("You were dealt a Seven"); break;
case 8: System.out.println("You were dealt a Eight"); break;
case 9: System.out.println("You were dealt a Nine"); break;
case 10: System.out.println("You were dealt a Ten"); break;
case 11: System.out.println("You were dealt a Ace"); break;
}
playerSum += card3;
}
}
当用户在询问他是否想要另一张卡片时输入 "no" 时,我希望循环结束。目前,如果我输入 "yes" 以外的任何内容,它将继续询问“您想要另一张卡片吗?”。我明白为什么会这样,但我不知道如何解决。提前致谢。
您只需添加
即可解决此问题
break;
所以它看起来像
if (no) { break;}
希望这对您有所帮助。
在 if 语句的末尾添加一个 else 并跳出循环。
if (input.equalsIgnoreCase("yes")){
int card3;
Random r3 = new Random();
card3 = r3.nextInt(11 - 2 + 1) + 2;
switch (card3) {
case 2: System.out.println("You were dealt a Two"); break;
case 3: System.out.println("You were dealt a Three"); break;
case 4: System.out.println("You were dealt a Four"); break;
case 5: System.out.println("You were dealt a Five"); break;
case 6: System.out.println("You were dealt a Six"); break;
case 7: System.out.println("You were dealt a Seven"); break;
case 8: System.out.println("You were dealt a Eight"); break;
case 9: System.out.println("You were dealt a Nine"); break;
case 10: System.out.println("You were dealt a Ten"); break;
case 11: System.out.println("You were dealt a Ace"); break;
}
playerSum += card3;
} else {
break;
}
注意: 当用户输入不同于 "yes" 的任何内容时,这将跳出循环。如果你想打破 only only,使用 else if
.
我有一个 21 点的 while 循环 Class:
while (playerSum < 21) {
System.out.println("Would you like another card?");
String input;
System.out.flush();
input = in.readLine();
if (input.equalsIgnoreCase("yes")){
int card3;
Random r3 = new Random();
card3 = r3.nextInt(11 - 2 + 1) + 2;
switch (card3) {
case 2: System.out.println("You were dealt a Two"); break;
case 3: System.out.println("You were dealt a Three"); break;
case 4: System.out.println("You were dealt a Four"); break;
case 5: System.out.println("You were dealt a Five"); break;
case 6: System.out.println("You were dealt a Six"); break;
case 7: System.out.println("You were dealt a Seven"); break;
case 8: System.out.println("You were dealt a Eight"); break;
case 9: System.out.println("You were dealt a Nine"); break;
case 10: System.out.println("You were dealt a Ten"); break;
case 11: System.out.println("You were dealt a Ace"); break;
}
playerSum += card3;
}
}
当用户在询问他是否想要另一张卡片时输入 "no" 时,我希望循环结束。目前,如果我输入 "yes" 以外的任何内容,它将继续询问“您想要另一张卡片吗?”。我明白为什么会这样,但我不知道如何解决。提前致谢。
您只需添加
即可解决此问题break;
所以它看起来像
if (no) { break;}
希望这对您有所帮助。
在 if 语句的末尾添加一个 else 并跳出循环。
if (input.equalsIgnoreCase("yes")){
int card3;
Random r3 = new Random();
card3 = r3.nextInt(11 - 2 + 1) + 2;
switch (card3) {
case 2: System.out.println("You were dealt a Two"); break;
case 3: System.out.println("You were dealt a Three"); break;
case 4: System.out.println("You were dealt a Four"); break;
case 5: System.out.println("You were dealt a Five"); break;
case 6: System.out.println("You were dealt a Six"); break;
case 7: System.out.println("You were dealt a Seven"); break;
case 8: System.out.println("You were dealt a Eight"); break;
case 9: System.out.println("You were dealt a Nine"); break;
case 10: System.out.println("You were dealt a Ten"); break;
case 11: System.out.println("You were dealt a Ace"); break;
}
playerSum += card3;
} else {
break;
}
注意: 当用户输入不同于 "yes" 的任何内容时,这将跳出循环。如果你想打破 only only,使用 else if
.