Java Do While is';t工作不正常
对不起,我是这个网站的新手,所以不确定这将如何显示。我想做一个简单的石头,布,剪刀游戏。在Java Do While is';t工作不正常,java,loops,do-while,freeze,continue,Java,Loops,Do While,Freeze,Continue,对不起,我是这个网站的新手,所以不确定这将如何显示。我想做一个简单的石头,布,剪刀游戏。在while语句之后,如果没有输入R、P、S,则程序什么也不做。我想让它回到问题的开头,这样就可以做出正确的选择。另外,如何输入打印语句,如“无效选择请重试” 看起来您在循环时忘记了一个内部的do 应该是: do { do { System.out.println("Welcome to Rock, Paper, Scissors Game."); System.out
while
语句之后,如果没有输入R、P、S,则程序什么也不做。我想让它回到问题的开头,这样就可以做出正确的选择。另外,如何输入打印语句,如“无效选择请重试”
看起来您在循环时忘记了一个内部的
do
应该是:
do {
do {
System.out.println("Welcome to Rock, Paper, Scissors Game.");
System.out.println("Pick R, P, or S.");
userChoice = sc.nextLine();
} while (!userChoice.equalsIgnoreCase("P") && !userChoice.equalsIgnoreCase("R") && !userChoice.equalsIgnoreCase("S"));
...
} while (playAgain.equalsIgnoreCase("Y"));
如果没有内部do
(以及环绕该循环体的花括号),内部循环将成为,而循环体为空。看起来您在循环时忘记了内部do
的一个do
应该是:
do {
do {
System.out.println("Welcome to Rock, Paper, Scissors Game.");
System.out.println("Pick R, P, or S.");
userChoice = sc.nextLine();
} while (!userChoice.equalsIgnoreCase("P") && !userChoice.equalsIgnoreCase("R") && !userChoice.equalsIgnoreCase("S"));
...
} while (playAgain.equalsIgnoreCase("Y"));
如果没有内部的do
(以及环绕该循环主体的花括号),内部循环将变成一个while
循环,主体为空。正如Eran所说,您需要将do-while循环包装到另一个循环中,这将不断要求用户输入正确的内容。这是完全有效的代码。一件可能更好的事情是用户输入错误的字母后的消息
编辑:还要确保为每次迭代绘制随机数
编辑2:要根据用户输入更改消息,您可以引入一个新变量,该变量将跟踪您请求用户正确输入的次数。如果为0,则表示第一次询问用户,我们应打印“欢迎”消息。它不是0-您需要向用户请求正确的输入。在每一轮之后,我们再次给变量赋值零,循环重复。我已经在代码中实现了这个更改。请注意,此变量也可以是布尔值
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String userChoice;
String playAgain;
int iterationNumber;
while (true) {
iterationNumber = 0;
do {
if (iterationNumber == 0) {
System.out.println("Welcome to Rock, Paper, Scissors Game.");
System.out.println("Pick R, P, or S.");
} else {
System.out.println("Please enter valid letter.");
System.out.println("Pick R, P, or S.");
}
iterationNumber++;
userChoice = sc.nextLine();
} while (!userChoice.equalsIgnoreCase("P")
&& !userChoice.equalsIgnoreCase("R")
&& !userChoice.equalsIgnoreCase("S"));
String compChoice = "";
int randNum = (int) (Math.random() * 3);
switch (randNum) {
case 0:
compChoice = "R";
break;
case 1:
compChoice = "P";
break;
case 2:
compChoice = "S";
break;
}
System.out.println("The computer entered \"" + compChoice + "\".");
if (compChoice.equalsIgnoreCase(userChoice)) {
System.out.println("Draw");
} else if (userChoice.equalsIgnoreCase("R")
&& compChoice.equalsIgnoreCase("S")
|| userChoice.equalsIgnoreCase("P")
&& compChoice.equalsIgnoreCase("R")
|| userChoice.equalsIgnoreCase("S")
&& compChoice.equalsIgnoreCase("P")) {
System.out.println("User Wins");
} else {
System.out.println("User Loses");
}
System.out.print(
"Do you want to play again? (Y/N)");
playAgain = sc.nextLine();
if (playAgain.equalsIgnoreCase("N")) {
break;
}
iterationNumber = 0;
}
System.out.println("Thanks for Playing!");
}
正如Eran所说,您需要将do-while循环包装到另一个循环中,这将不断要求用户提供正确的输入。这是完全有效的代码。一件可能更好的事情是用户输入错误的字母后的消息
编辑:还要确保为每次迭代绘制随机数
编辑2:要根据用户输入更改消息,您可以引入一个新变量,该变量将跟踪您请求用户正确输入的次数。如果为0,则表示第一次询问用户,我们应打印“欢迎”消息。它不是0-您需要向用户请求正确的输入。在每一轮之后,我们再次给变量赋值零,循环重复。我已经在代码中实现了这个更改。请注意,此变量也可以是布尔值
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String userChoice;
String playAgain;
int iterationNumber;
while (true) {
iterationNumber = 0;
do {
if (iterationNumber == 0) {
System.out.println("Welcome to Rock, Paper, Scissors Game.");
System.out.println("Pick R, P, or S.");
} else {
System.out.println("Please enter valid letter.");
System.out.println("Pick R, P, or S.");
}
iterationNumber++;
userChoice = sc.nextLine();
} while (!userChoice.equalsIgnoreCase("P")
&& !userChoice.equalsIgnoreCase("R")
&& !userChoice.equalsIgnoreCase("S"));
String compChoice = "";
int randNum = (int) (Math.random() * 3);
switch (randNum) {
case 0:
compChoice = "R";
break;
case 1:
compChoice = "P";
break;
case 2:
compChoice = "S";
break;
}
System.out.println("The computer entered \"" + compChoice + "\".");
if (compChoice.equalsIgnoreCase(userChoice)) {
System.out.println("Draw");
} else if (userChoice.equalsIgnoreCase("R")
&& compChoice.equalsIgnoreCase("S")
|| userChoice.equalsIgnoreCase("P")
&& compChoice.equalsIgnoreCase("R")
|| userChoice.equalsIgnoreCase("S")
&& compChoice.equalsIgnoreCase("P")) {
System.out.println("User Wins");
} else {
System.out.println("User Loses");
}
System.out.print(
"Do you want to play again? (Y/N)");
playAgain = sc.nextLine();
if (playAgain.equalsIgnoreCase("N")) {
break;
}
iterationNumber = 0;
}
System.out.println("Thanks for Playing!");
}
对非常感谢。这让我抓狂。如果输入R、P或R不满足while条件,我如何让它打印“无效”S@KenYoung然后,您需要在do while循环中添加一个if
,以检查此项。是!非常感谢。这让我抓狂。如果输入R,P不满足while条件,我如何让它打印“无效”,或S@KenYoung然后,您需要在do while循环中添加一个if
来检查这一点。当我输入消息让用户知道他们输入了无效的选项时,当他们输入正确的选项时,它似乎会打印消息。这很有意义。一般来说,我对Java和编码是相当陌生的。谢谢你的帮助!当我输入消息让用户知道他们输入了无效的选项时,当他们输入正确的选项时,它似乎会打印消息。这是有道理的。一般来说,我对Java和编码是相当陌生的。谢谢你的帮助!