在Java的岩石纸剪刀

时间:2013-09-19 01:24:21

标签: java

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

之下

import 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 );


  }


}

9 个答案:

答案 0 :(得分:4)

  

我想这样做,以便用户可以输入“Rock”或“paper”而不是1,2和3

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

  

我应该使用嵌套的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.
    }
}

答案 1 :(得分: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...

答案 2 :(得分: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;
            }
        }

答案 3 :(得分:0)

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

Lookup enum by string value

至于嵌套的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
}

答案 4 :(得分: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...

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

答案 5 :(得分:0)

您可以使用.equalsIgnoreCase();来检查“Rock”,“Paper”,“etc”,如果用户输入“rock”,“PaPER”等,它甚至会匹配。同时添加嵌套的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();

希望对你有用!

答案 6 :(得分:0)

经过一番思考,这就是你能做的,

创建包含元素的枚举

enum Elements{ROCK ,PAPER ,SCISSORS};

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

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对象的决定赢家方法即可获得获胜的玩家。

答案 7 :(得分: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);
 }

}

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

答案 8 :(得分: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;
    }
}