所以我正在制作每个人都很有趣的游戏“Rock,Paper,Scissors”我让一切正常,除了让while循环在停止之前重复3次。那么它会重复3次并停止,但第2次和第3次重复变量不会改变。看看代码并告诉我我做错了什么。 **更新:现在我已经完成了所有工作,如何获得这个“Q”字符串来终止循环?
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
/**
* (Insert a brief description that describes the purpose of this method)
*
* @param args
*/
public static void main(String[] args)
{
int compint;
String usermove = "";
String compmove = "";
String winner = "";
int count = 0;
Scanner in = new Scanner(System.in);
Random gen = new Random();
System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
int input = in.nextInt();
while (count < 3)
{
compint = gen.nextInt(3) + 1;
if (input == 1)
{
usermove = "Rock";
}
else if (input == 2)
{
usermove = "Paper";
}
else if (input == 3)
{
usermove = "Scissors";
}
if (compint == 1)
{
compmove = "Rock";
}
else if (compint == 2)
{
compmove = "Paper";
}
else if (compint == 3)
{
compmove = "Scissors";
}
if (compint == input)
{
winner = "TIE";
}
else if (compint == 1 && input == 3)
{
winner = "COMPUTER";
}
else if (compint == 2 && input == 1)
{
winner = "COMPUTER";
}
else if (compint == 3 && input == 2)
{
winner = "COMPUTER";
}
else
{
winner = "USER";
}
System.out.print("Computer: " + compmove + " | ");
System.out.print("You: " + usermove + " | ");
System.out.println("Winner: " + winner);
System.out.println();
System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input = in.nextInt();
count++;
}
}
}
输出:
Enter Rock(1), Paper(2), Scissors(3) {Q to quit]:
1
Computer: Scissors | You: Rock | Winner: USER
Enter Rock(1), Paper(2), Scissors(3) {Q to quit]:
2
Computer: Rock | You: Paper | Winner: USER
Enter Rock(1), Paper(2), Scissors(3) {Q to quit]:
Q
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at RockPaperScissors.main(RockPaperScissors.java:102)
答案 0 :(得分:3)
实际使用输入任何内容的逻辑 - 所有那些if
语句 - 都在循环之外。通过每次迭代看,没有任何逻辑实际上没有被执行。一切都只是先发生。试试这个:
for (int count=0; count < 3; count++)
{
int input = in.nextInt();
int compint = gen.nextInt(3) + 1;
// all the if statements and printing here
}
**更新:既然我已经完成了所有工作,我如何获得这个“Q”字符串来终止循环?
您在输入Q
时收到InputMismatchException
,但代码会调用Scanner#nextInt()
。文档很清楚问题是什么:
由
Scanner
抛出,表示检索到的令牌与预期类型的模式不匹配,或者令牌超出预期类型的范围。
这基本上是Scanner
告诉你的方式“你要求int
,但下一个标记不是一个。”您可以在nextInt()
调用之前使用Scanner#hasNextInt()
添加其他检查,以验证实际的下一个标记是否为int。如果它不是int,那么你可以计划将其解析为字符串。
所以不要这样:
input = in.nextInt();
做这样的事情:
if (in.hasNextInt())
{
input = in.nextInt();
} else if (in.hasNext("Q")) {
// quit
}
答案 1 :(得分:1)
好像你想用do - while loop
它会工作:
do{
compint = gen.nextInt(3) + 1;
System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
int input = in.nextInt();
if (input == 1)
{
usermove = "Rock";
}
else if (input == 2)
{
usermove = "Paper";
}
else if (input == 3)
{
usermove = "Scissors";
}
if (compint == 1)
{
compmove = "Rock";
}
else if (compint == 2)
{
compmove = "Paper";
}
else if (compint == 3)
{
compmove = "Scissors";
}
if (compint == input)
{
winner = "TIE";
}
else if (compint == 1 && input == 3)
{
winner = "COMPUTER";
}
else if (compint == 2 && input == 1)
{
winner = "COMPUTER";
}
else if (compint == 3 && input == 2)
{
winner = "COMPUTER";
}
else
{
winner = "USER";
}
System.out.print("Computer: " + compmove + " | ");
System.out.print("You: " + usermove + " | ");
System.out.println("Winner: " + winner);
System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
input = in.nextInt();
count++;
}while (count < 3);
答案 2 :(得分:0)
特别是,你的“while”循环除了打印之外不对胜利者做任何事情,因此无论输入如何,赢家对于整个游戏都保持不变。这可能是开始“调试”的好地方。