我目前正在编写一个简单的java文本游戏,你会用摇滚剪刀在电脑上玩电脑。
我无法弄清楚为什么我的代码无法正常工作。我确定我做了一些愚蠢的事情。请帮忙。
System.out.println("Welcome to rock paper scissors! select 'Rock', 'Paper', or 'Scissors'");
Scanner scan = new Scanner(System.in);
while(playerscore <2 && computerscore <2 ){
String player = scan.next();
System.out.println("you chose "+player+".");
//convert lowercase to capital
if(player.equals("rock")){
player = "Rock";
}
else if(player.equals("paper")){
player = "Paper";
}
else if(player.equals("scissors")){
player = "Scissors";
}
Random ran = new Random();
int computer = ran.nextInt(3);
if(computer == 0){
System.out.println("Computer chose rock.");
if(player.equals("Rock")) {
tie();
}
else if (player.equals("Paper")){
lose();
}
else if(player.equals("Scissors")){
win();
}
}
else if(computer == 1){
System.out.println("Computer chose paper.");
if(player.equals("Rock")){
win();
}
else if (player.equals("Paper")){
tie();
}
else if(player.equals("Scissors")){
lose();
}}
else if(computer == 2){
System.out.println("Computer chose paper.");
if(player.equals("Rock")){
lose();
}
else if (player == "Paper"){
win();
}
else if(player.equals("Scissors")){
tie();
}}
}
}
public static int playerscore = 0;
public static int computerscore = 0;
public static void tie(){
System.out.println("You and the computer tied!");
return;
}
public static void win(){
System.out.println("You won that round!");
playerscore++;
System.out.println("you now have "+playerscore+" points");
return;
}
public static void lose(){
System.out.println("You lost that round!");
computerscore++;
System.out.println("The computer now has "+ computerscore + " points");
}
`