0

所以我制作了每个人的有趣游戏“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)
4

3 回答 3

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”字符串终止循环?

您在InputMismatchException键入时得到一个,Q但代码调用Scanner#nextInt(). 文档非常清楚问题是什么:

由 a 抛出Scanner以指示检索到的令牌与预期类型的​​模式不匹配,或者该令牌超出预期类型的​​范围。

这基本上是Scanner's 告诉你“你要求一个int但下一个令牌不是一个”的方式。nextInt()您可以在调用之前添加一个额外的检查,使用Scanner#hasNextInt(), 来验证下一个令牌实际上一个 int。如果它不是 int,那么您可以计划将其解析为字符串。

所以代替这个:

input = in.nextInt();

做这样的事情:

if (in.hasNextInt())
{
    input = in.nextInt();
} else if (in.hasNext("Q")) {
    // quit
}
于 2013-02-10T03:28:19.120 回答
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);
于 2013-02-10T03:35:21.187 回答
0

特别是,您的“while”循环除了打印它之外不会对获胜者做任何事情,因此无论输入如何,整个游戏的获胜者都保持不变。这可能是开始“调试”的好地方。

于 2013-02-10T03:33:24.993 回答