Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 爪哇的石头剪刀_Java - Fatal编程技术网

Java 爪哇的石头剪刀

Java 爪哇的石头剪刀,java,Java,我正在用java写一个石头剪刀布游戏,但有些事情我搞不懂。首先,我想让用户可以输入Rock或paper,而不是1、2和3,但我想不出来。其次,我应该使用嵌套的if-else语句,但我也不知道如何使用我一直在做的事情。我的代码在下面 导入java.util.Scanner; 公共级摇滚游戏{ private final static int ROCK=1; private final static int PAPER =2; private final static int SCISS

我正在用java写一个石头剪刀布游戏,但有些事情我搞不懂。首先,我想让用户可以输入Rock或paper,而不是1、2和3,但我想不出来。其次,我应该使用嵌套的if-else语句,但我也不知道如何使用我一直在做的事情。我的代码在下面

导入java.util.Scanner; 公共级摇滚游戏{

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


  }


}
使用:

然后:

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...
使用:

然后:

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...
我想让用户可以输入岩石或纸张,而不是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.
    }
}
我想让用户可以输入岩石或纸张,而不是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.
    }
}

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

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

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

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

首先,您不需要这些行:

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。

首先,您不需要这些行:

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。

您可以使用.equalsIgnoreCase;检查岩石、纸张等,或者如果用户输入岩石、纸张等,它甚至会匹配。另外嵌套if语句中的ng将使函数更加高效。要生成嵌套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

希望这对您有所帮助!

您可以使用.equalsIgnoreCase;来检查岩石、纸张等,或者如果用户输入岩石、纸张等,它甚至会匹配。此外,添加嵌套的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


希望这对你有用!

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

使用元素创建枚举

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对象的Decise winner方法来获得获胜的玩家。

经过一些思考后,您可以这样做

使用元素创建枚举

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对象的Decise winner方法来获得获胜的玩家。

在我个人的选择中,所有给定的答案都会使事情复杂化

下面是一个非常简单的示例,它存储了一个简单的二维数组


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

在我的个人选项中,所有给定的答案都通过co
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;
    }
}
使事情复杂化

下面是一个非常简单的示例,它存储了一个简单的二维数组


数组存储所有可能的结果

对不起,我真的是新手,第一行做什么?它从命令行读取一行。。。并将其存储到val字符串中。当然,如果愿意,您可以使用另一个变量名。你不必感到抱歉,我们都是从这个层次开始的。。。尽管我不得不说,回头看,代码看起来并不优雅。但无意冒犯:DNice新型循环,如果循环!:toLower将字符串转换为小写字符。所以岩石变成了岩石。@Diego C Nascimento:在荷兰语中,有些书说如果卢森。字面翻译意味着if循环。此外,这并不罕见:很抱歉,我真的是新手,第一行做什么?它从命令行读取一行。。。并将其存储到val字符串中。当然,如果愿意,您可以使用另一个变量名。你不必感到抱歉,我们都是从这个层次开始的。。。尽管我不得不说,回头看,代码看起来并不优雅。但无意冒犯:DNice新型循环,如果循环!:toLower将字符串转换为小写字符。所以岩石变成了岩石。@Diego C Nascimento:在荷兰语中,有些书说如果卢森。字面翻译意味着if循环。此外,这并不少见:如果您希望用户键入字符串,则使用字符串而不是整数。下面的答案解释了如何操作。您使用key.nextInt,因此必须输入数字。如果使用key.next,则可以输入字符串。您应该在计算机与用户逻辑中使用else if结构,因为所有对都是互斥的。如果您希望用户键入字符串,则使用字符串而不是整数。下面的答案解释了如何操作。您使用key.nextInt,因此必须输入数字。如果使用key.next,则可以输入字符串。你应该在你的计算机和用户逻辑中使用else if结构,因为所有对都是互斥的。你帮了很大的忙,但无论出于什么原因,我的代码现在都无法工作。我知道我可能是个白痴,但你能看出问题所在吗?我编辑了代码以反映我现在拥有的内容。在初始化扫描仪之前,请使用密钥。因此,移动表示String val=key.nextLine.toLowerCase的行;向下至下方键=新扫描系统.in;。你帮了大忙,但不管出于什么原因,我的代码现在无法工作。我知道我可能是个白痴,但你能看出问题所在吗?我编辑了代码以反映我现在拥有的内容。在初始化扫描仪之前,请使用密钥。因此,移动表示String val=key.nextLine.toLowerCase的行;向下至下方键=新扫描系统.in;。
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;
    }
}