My Rock Paper Scissors游戏不会显示关系

时间:2014-02-14 01:10:36

标签: java

我在BlueJ写了这个岩石剪刀代码,到目前为止一切都很好......除了一件事。当我与电脑绑在一起时,游戏不会告诉我。它会告诉我计算机使用了什么,它会告诉我我是赢还是输。但是,如果我已经绑定,它将简单地告诉我计算机使用了什么并忽略了领带。代码如下。 ~~~

/**
*A simple game of Rock Paper Scissors.
*
*@author Erik Ingvoldsen
*@version 1.0
*/

import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String playAgain = "4";    
do
    {
    String personPlay;
    String computerPlay = "";
    int computerInt;
    String response;
    Scanner scan = new Scanner(System.in);
    Random generator = new Random();
    System.out.println("   ");
    System.out.println("Press 1 for Rock, 2 for Scissors, 3 for Paper, and 4 to quit.");
    System.out.println();
    computerInt = generator.nextInt(3)+1;
    if (computerInt == 1)
       computerPlay = "Rock";
    else if (computerInt == 2)
       computerPlay = "Scissors";
    else if (computerInt == 3)
       computerPlay = "Paper";
    //1=Rock 2=Scissors 3=Paper
    {System.out.println("Rock Paper Scissors: ");
    {personPlay = scan.next();
    System.out.println("Computer chooses: " + computerPlay);
    if (personPlay.equals(computerPlay)){ 
       System.out.println("It's a tie!");}
    else if (personPlay.equals("1")){
       if (computerPlay.equals("Paper"))
          System.out.println("Paper beats rock. Loser.");
    else if (computerPlay.equals("Scissors"))
            System.out.println("Rock beats scissors. Winner.");}
    else if(personPlay.equals("2")){
       if (computerPlay.equals("Paper"))
          System.out.println("Scissors beats paper. Winner");
    else if (computerPlay.equals("Rock"))
            System.out.println("Rock beats scissors. Loser.");}
   else if (personPlay.equals("3")) {
       if (computerPlay.equals("Rock"))
          System.out.println("Paper beats rock. Winner.");
   else if (computerPlay.equals("Scissors"))
            System.out.println("Scissors beats paper. Loser.");}
   else
System.out.println("Not a valid key. Cheaters never win.");}
System.out.println("Press 4 to quit or any other key to continue.");
Scanner End = new Scanner(System.in);
String quit=End.nextLine();
if(quit.equals("4"))
{
System.exit(0);}
}
}while(true);
}
}

3 个答案:

答案 0 :(得分:2)

您似乎正在尝试将"1""Paper"进行比较并期待true

进行两次比较true,即将用户输入与"1"和计算机比较为"Paper",或将"1"翻译为"Paper"(以及相应的其他两个值)并直接比较响应。

答案 1 :(得分:0)

personPlay可以是1,2或3。 computerPlay可以是Paper,Rock或Scissors。

因此,行f (personPlay.equals(computerPlay)){毫无意义。

答案 2 :(得分:0)

如前所述,您正在尝试将数字与此行的字符串进行比较

personPlay.equals(computerPlay)

此处personPlay是此人输入的数字(以字符串形式读入),computerPlay是您根据计算机获得的数字computerInt设置的字符串。

简单修复是更改上述比较以检查computerInt

的字符串值
personPlay.equals(String.valueOf(computerInt))

所以比较数字(如字符串)