1

我正在用java编写一个石头剪刀布游戏,但有些事情我无法弄清楚。首先,我想让它让用户可以输入“Rock”或“paper”而不是 1、2 和 3,但我想不通。其次,我应该使用嵌套的 if else 语句,但我也不知道如何用我一直在做的事情来做到这一点。我的代码如下

导入 java.util.Scanner;公共类 RockGame {

  private final static int ROCK=1;
  private final static int PAPER =2;
  private final static int SCISSOR =3;

  private static Scanner key;




public static void main(String args[]){
         int playerOneScore = 0;
         int computerScore = 0;

         int userPlay, computerPlay;


        String val = key.nextLine().toLowerCase();

         key = new Scanner(System.in);
         while(playerOneScore <2 && computerScore <2){


                 System.out.println("Choose 1 for rock, 2 for paper, and 3 for sciscors!");
                 userPlay = key.nextInt();
                 computerPlay = (int)(Math.random()*3) +1;
                 if(val.equals("rock"))
                       userPlay = ROCK;

                 else if (val.equals("paper"))
                         userPlay =PAPER;

                 else if (val.equals("scissors"))
                         userPlay=SCISSOR;


                 if (val.equals("rock"))
                         computerPlay = ROCK;
                 else if (val.equals("paper"))
                         computerPlay =PAPER;
                 else if (val.equals("scissors"))
                         computerPlay=SCISSOR;

                 if (computerPlay ==ROCK && userPlay==SCISSOR ){
                         System.out.println("The computer chose rock, you chose scissors.\n You lose!");
                         computerScore++;
                 }
                 if (computerPlay ==ROCK && userPlay==PAPER ){
                         System.out.println("You computer chose rock, you chose paper.\n You win!");
                         playerOneScore++;
                 }
                 if (computerPlay ==PAPER && userPlay==SCISSOR ){
                        System.out.println("The computer chose scissors, you chose paper.\n You win!");
                         playerOneScore++;
                 }
                 if (computerPlay ==PAPER && userPlay==ROCK ){
                         System.out.println("The computer chose paper and you chose rock. \n You lose!");
                         computerScore++;
                 }
                 if (computerPlay ==SCISSOR && userPlay==ROCK ){
                         System.out.println("The computer chose scissors and you chose rock. \n You win!");
                         playerOneScore++;
                 }
                 if (computerPlay ==SCISSOR && userPlay==PAPER ){
                         System.out.println("The computer chose scissors and you chose paper. \n You lose!");
                         computerScore++;
                 }
                 if (computerPlay == userPlay ){
                         System.out.println("The computer chose the same thing you did! \n Tie!");

                 }


         }
         if(computerScore > playerOneScore)
                 System.out.println("Computer win score is: - "+ computerScore + " -" + playerOneScore  );
         else
                 System.out.println("Your score is: " + playerOneScore + "-" + computerScore );


  }


}
4

9 回答 9

4

我想让它让用户可以输入“Rock”或“paper”而不是 1、2 和 3

使用key.nextLine().toLower(),然后测试该值是否等于“rock”等。

我应该使用嵌套的 if else 语句

请注意您的代码:

if (computerPlay ==SCISSOR && userPlay==ROCK ){
    // etc.
}
if (computerPlay ==SCISSOR && userPlay==PAPER ){
    // etc.
}

你检查是否computerPlay == SCISSOR两次。而使用嵌套语句,您可以执行以下操作:

if (computerPlay == SCISSOR) {
    if (userPlay == ROCK) {
        // etc.
    else if (userPlay == PAPER) {
        // etc.
    }
}
于 2013-09-19T01:31:02.217 回答
2

利用:

string val = sc.nextLine().toLower();

接着:

if(val.equals("rock") {
   userPlay = ROCK;
}
else if(...) {
   //..
}

您可以使用嵌套的 if 循环,例如:

if(userPlay == ROCK) {
  if(computerPlay == ROCK) {
    System.out.println("The computer chose the same thing you did! \n Tie!");
  }
  else if(computerPlay == PAPER) {
    System.out.println("The computer chose paper and you chose rock. \n You lose!");
    computerScore++;
  }
  else {
    System.out.println("The computer chose scissors and you chose rock. \n You win!");
    playerOneScore++;
  }
}
else if(userPlay == PAPER) {
  if(computerPlay == ROCK) {
    System.out.println("You computer chose rock, you chose paper.\n You win!");
    playerOneScore++;
  }
  else if(computerPlay == PAPER) {
    System.out.println("The computer chose the same thing you did! \n Tie!");
  }
  else {
    System.out.println("The computer chose scissors and you chose paper. \n You lose!");
    computerScore++;
  }
}
//I think you get the idea...
于 2013-09-19T01:27:41.993 回答
1

尝试这个

        // paper == 0
        // rock == 1
        // scissors == 2
        System.out.println("Select 0 for Paper \nSelect 1 for Rock \nSelect 2 for Scissors");
        Scanner scan = new Scanner(System.in);
        int player = scan.nextInt();
        Random comp = new Random();
        int com = comp.nextInt(2);
        System.out.println(com);
        if (player == com){
            System.out.println("Match is Tie");
        }else{
            switch (com){
                case 0:
                    if(player == 2){
                        System.out.println("Player Wins the Match");
                    }else{
                        System.out.println("Comp wins the Match");
                    }
                    break;
                case 1:
                    if(player == 0){
                        System.out.println("Player Wins the Match");
                    }else{
                        System.out.println("Comp wins the Match");
                    }
                    break;
                case 2:
                    if(player == 1){
                        System.out.println("Player Wins the Match");
                    }else{
                        System.out.println("Comp wins the Match");
                    }
                    break;
            }
        }
于 2021-07-25T03:05:01.407 回答
0

您可以使用.equalsIgnoreCase();检查“Rock”、“Paper”、“etc”,或者如果用户输入“rock”、“PaPER”等,它甚至会匹配。还添加嵌套的 if 语句将使您的函数更加高效。要创建一个嵌套的 if 语句,您需要做的就是将一个 if 语句放在另一个 if 语句中,如下所示。

    if(userPlay.equalsIgnoreCase("Rock")) {

         if (computerPlay.equalsIgnoreCase("Scissors"){
                System.out.println("The computer chose scissors, you chose rock.\n You win!");
                             playerOneScore++;
         }
         else if (computerPlay.equalsIgnoreCase("Paper")){
                System.out.println("You computer chose paper, you chose rock.\n You lose!");
                             computerScore--;
         }
     }

使用这些嵌套的 if 语句,您只需为每个选项检查一次用户选择,而不是像示例代码中那样多次检查。

您还需要从更改为key.nextInt()key.next()以便将字符串作为输入并将它们存储在userPlay而不是整数中

userPlay = key.next();

希望对你有一些用处!

于 2013-09-19T01:35:23.677 回答
0

首先,您需要创建一个枚举,然后将用户的输入解析到该枚举中:

按字符串值查找枚举

至于嵌套的 if-else 语句,从技术上讲

if(something){
    // do something
}else if(somethingElse){
    // do something else
}else{
    // do another things
}

是一个嵌套的 if-else 语句。至少在编译器看来是这样的。

我怀疑你的老师希望看到这样的结构:

if(computerPlay == ROCK){
    if(userPlay == PAPER){
    }
    else if(userPlay == ROCK){
    }
    else if(userPlay == SCISSOR){
    }
}
else if(computerPlay == PAPER){
    // same as above
}
else if(computerPlay == SCISSOR){
    // same as above
}
于 2013-09-19T01:33:51.430 回答
0

在一些人认为这是你可以做的之后,

使用元素创建枚举

enum Elements{ROCK ,PAPER ,SCISSORS};

为元素的各种组合创建第二个枚举,并实现一个接口,

enum ElementCombinations implements RockPaperScissorssLogic
{
    ROCK_PAPER
    {
        int decideWinner()
        {
            //For second player
            return 2;
        }
    }, 
    PAPER_SCISSORS
    {
        int decideWinner()
        {
            //For second player
            return 2;
        }
    }, 
    ..... ;

    // For obtaining decide the type of combination object to be used.
    public static ElementCombinations getCombination(Element player1, Element player2)
    {
        // Some logic...
    }
};

最后,在您的代码中,您可以简单地调用 ElementCombinations 对象的决定获胜者方法来获取获胜玩家。

于 2013-09-19T03:30:44.580 回答
0

首先,您不需要这些行:

if (computerPlay==1)
    computerPlay = ROCK;
if (computerPlay==2)
    computerPlay =PAPER;
if (computerPlay==3)
    computerPlay=SCISSOR;

其次,你说你应该使用嵌套的 if/else 语句——也许这就是你的老师所追求的:

if (computerPlay == ROCK)
{
    if (userPlay==ROCK)
    {
        ...
    }
    else if (userPlay == PAPER)
    {
        ...
    }
    else if (userPlay == SCISSORS)
    {
        ...
    }
    else
    {
        // Cheating!
    }
}
else if (computerPlay == PAPER)
{
    if (userPlay==ROCK)
    {
        ...
    }
    // And so on...

这是一种非常冗长的做事方式(我个人会进行二维数组查找)并且您的原始代码看起来可以接受,但我想这是一个学习练习,让您习惯嵌套 if/else .

于 2013-09-19T01:34:49.567 回答
0

在我个人的选择中,所有给出的答案都使事情变得过于复杂。

这是一个非常简单的示例,通过存储一个简单的 2-d array

public class Main{

   public final static int SCISSORS = 0;
   public final static int PAPER = 1;
   public final static int STONE = 2;

   public static void rockPaperScissors(int player1, int player2){
        if(player1 == player2){ System.out.println("Tie"); return;}

        boolean key[][] = {
             {false, true, false},
             {false, false, true},
             {true, false, false} 
        };

        boolean player1Wins = key[player1][player2];

        if(player1Wins){ System.out.println("Player one wins"); }
        else{ System.out.println("Player two wins"); } 

   }

  public static void main(String[] arrgs){

       rockPaperScissors(SCISSORS, STONE);
 }

}

该数组存储所有可能的结果。

于 2018-01-15T10:45:47.880 回答
0
import java.util.Scanner;

public class RockPaperScissors {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Hey let's play Rock, Paper, Scissors!");
System.out.println("You must choose 1 for Rock, 2 for Paper and 3 for Scissors");
System.out.print("Rock, Paper or Scissors? ");
int play = console.nextInt();
String user = intToName(play,console);
int person = play;
int computer = generateRandomNumber(1,3);

}
    public static String intToName(int play,Scanner console){
    String choose = "";
     if (play == -1){
         System.out.print("Game Over!");
     }
    while (play != -1){
    if (play == 1){
        System.out.println("Your play is: Rock");
         if (generateRandomNumber(1,3) == 1){
                System.out.println("Computer player is: Rock");  
                System.out.println("It's a tie!");
              }
         else if (generateRandomNumber(1,3) == 2){
                    System.out.println("Computer player is: Paper"); 
                    System.out.println("Paper eats Rock. You lose!");
              }
           else if (generateRandomNumber(1,3) == 3){
                    System.out.println("Computer player is: Scissors"); 
                    System.out.println("Rock eats Scissors. You win!");
              }
    }
    else if (play == 2){
        System.out.println("Your play is: Paper");
         if (generateRandomNumber(1,3) == 1){
                System.out.println("Computer player is: Rock");  
                System.out.println("Paper eats Rock. You win!");
              }
         else if (generateRandomNumber(1,3) == 2){
                    System.out.println("Computer player is: Paper");  
                    System.out.println("It's a tie!");
              }
         else  if (generateRandomNumber(1,3) == 3){
                    System.out.println("Computer player is: Scissors"); 
                    System.out.println("Scissors eats Paper. You lose!");
              }
    }
    else if (play == 3){
     System.out.println("Your play is: Scissors");
     if (generateRandomNumber(1,3) == 1){
            System.out.println("Computer player is: Rock"); 
            System.out.println("Rock eats Scissors. You lose!");
          }
     else if (generateRandomNumber(1,3) == 2){
                System.out.println("Computer player is: Paper");  
                System.out.println("Scissors eats Paper. You win!");
          }
     else if (generateRandomNumber(1,3) == 3){
                System.out.println("Computer player is: Scissors");  
                System.out.println("It's a tie!");
          }
    }
    System.out.println();
    System.out.print("Rock, Paper or Scissors? ");
   play = console.nextInt();
  }
    return choose;
    }
    public static int generateRandomNumber(int a, int b){
        return a+(int)(Math.random()*b-a+1);
    }
    public static int winner(int person, int computer){
        int win =0;
        int lose =0;
        int tie=0;
        if (person == 1 && computer == 1){
            tie++;
        }
        if (person == 2 && computer == 2){
            tie++;
        }
        if (person == 3 && computer == 3){
            tie++;
        }
        if (person == 1 && computer == 2){
             lose++;
        }
        if (person == 1 && computer == 3){
            win++;
        }
        if (person == 2 && computer == 1){
            win++;
        }
            if (person == 2 && computer == 3){
                lose++;
    }
            if (person == 3 && computer == 1){
                lose++;
            }
            if (person == 3 && computer == 2){
                win++;
            }
            return win;
    }
}
于 2018-04-15T15:28:05.420 回答