需要帮助修复此Rock Paper Scissors游戏

时间:2015-04-01 02:46:56

标签: java

以下代码未显示任何错误,但是,当我尝试输入user play时,我遇到了大量错误。

public static void rockPaperScissors(Scanner input) {
    // TODO Auto-generated method stub
    String user = null, computer = null;
    int computerRand;
    Random num = new Random();
    System.out.println("Lets play Rock, Paper, Scissors.\nPlease enter a move\n");
    computerRand=num.nextInt(3)+1;

    if (computerRand==1)
        computer="R";
    else if (computerRand==2)
        computer="P";
    else if (computerRand==3)
        computer="S";

    System.out.println("Enter user play");
    user=input.nextLine();

    if (user==computer){
        System.out.println("Tie. Try over!");
        }
    else if (user==("R")){
        if (computer==("S"))
            System.out.println("Rock beats Scissors. You win!");
    else if (computer==("P"))
            System.out.println("Paper covers rock. CPU wins..");
    }
    else if (user==("P")){
        if (computer==("S"))
            System.out.println("Scissors cut paper. CPU wins..");
    else if (computer==("R"))
            System.out.println("Paper covers rock. You win!");
    }
    else if (user==("S")){
        if (computer==("P"))
            System.out.println("Scissors cuts paper. You win!");
    else if (computer==("R"))
            System.out.println("Rock beats scissors. CPU wins..");
    }

            System.out.println("Computer play is: "+computer);

}

1 个答案:

答案 0 :(得分:1)

  "else if (user==("R")){"

这里你要用'=='比较字符串这不好我想尝试

(user.equals("R")) 

会有所帮助。

结帐this question