我正在用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 );
}
}