Rock Paper Scissors计划出错

时间:2014-04-06 19:37:58

标签: java

最初的提示让我们制作了一个石头剪刀游戏我认为我的代码到目前为止应该工作但是我在第64行遇到错误“找不到符号”当我试图追踪是否发生了平局。有谁知道我做错了什么?

    import java.util.*;
    public class RPS
    {
        public static void main(String[] args)
        {
           Scanner myScanner = new Scanner(System.in);
           Random myRandom = new Random();
           boolean n = false;
           System.out.println("Welcome to the Rock, Paper, Scissors game!!");
           System.out.println("When you play, your choices are rock, paper, or     scissors");       
           do
           {
            int compChoice = myRandom.nextInt(3);
            String rock = "0";
            String paper = "1";
            String scissors = "2";
            Integer.parseInt(rock);
            Integer.parseInt(paper);
            Integer.parseInt(scissors);
            int compWins = 0;
            int humanWins = 0;
            int ties = 0;
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(myScanner.nextLine());
            for(int i = 0; i < 3; i++)
            {  
                if(choice == 0 && compChoice == 1)
                {
                    System.out.println("I choose paper: I win this turn");
                    i++;
                    compWins++;
                } else if(choice == 0 && compChoice == 2)
                {
                    System.out.println("I choose scissors: You win this turn");
                    i++;
                    humanWins++;
                } else if(choice == 1 && compChoice == 0)
                {
                    System.out.println("I choose rock: You win this turn");
                    i++;
                    humanWins++;
                }   else if(choice == 1 && compChoice == 2)
                {
                    System.out.println("I choose scissors: I win this turn");
                    i++;
                    compWins++;
                } else if(choice == 2 && compChoice == 0)
                {
                    System.out.println("I choose rock: I win this turn");
                    i++;
                    compWins++;
                } else if(choice == 2 && compChoice == 1)
                {
                    System.out.println("I choose paper: You win this turn");
                    i++;
                    humanWins++;
                } else if(choice == compchoice)
                {
                    System.out.println("This round is a tie");
                    i++;
                    ties++;
                }    
                System.out.println("Score: me: " + compWins + "you: " + humanWins +  "ties: " + ties);
            }  
                if(compWins > humanWins)
                {
                    System.out.println("Game over: I win!");
                } else if(humanWins > compWins)
                {
                    System.out.println("Game over: You win!");
                }else
                    System.out.println("Game over: No one wins!");
            System.out.print("Play again? (y/n)");
            String ending = myScanner.nextLine();
            if(ending == n)
            {
                n = true;
            }   
           }
           while(!n);
           System.out.println("Thank you for playing!");
           System.exit(0);
        }    
    }

3 个答案:

答案 0 :(得分:1)

compchoice不是骆驼案。您将其声明为compChoice

答案 1 :(得分:1)

您有2个错误:

  • Java区分大小写,您使用的compchoice未在任何地方声明。我想您想要使用compChoice代替:

    } else if (choice == compChoice) {
    
  • 您正在比较行中的Stringboolean

    if (ending == n) {
    

    哪个不是有效的比较,因为它们来自不同的类型。你可能想检查那里的逻辑。

答案 2 :(得分:0)

实际上Java是大小写的,当你声明一个变量时,你必须坚持使用该声明。

所以试着改变:

} else if(choice == compChoice) // here you need to change compchoice to compChoice
            {
                System.out.println("This round is a tie");
                i++;
                ties++;
            }    
            System.out.println("Score: me: " + compWins + "you: " + humanWins +  "ties: " + ties);

此外,您将String与布尔值进行比较,您需要将String解析为boolean,然后比较:

boolean ending = Boolean.valueOf(myScanner.nextLine());
        if(ending == n)
        {
            n = true;
        }