希望这是一个快速的问题。我正在制作一个简单的石头剪刀布游戏。我做一个随机的计算机选择很好,让用户的选择很好。但是当我试图找出谁获胜时,它会在我的 else if 块中打印最后一个 else if 用于无效输入。
当做出正确的选择时,它会打印“输入一个有效的选择”。
import java.util.Random;
import javax.swing.JOptionPane;
public class JavaApplication4 {
public static void main(String[] args)
{
Random ranNums = new Random();
int comp = ranNums.nextInt(3);
String comp2;
String winner;
String user = JOptionPane.showInputDialog
(null, "Enter rock, paper, or scissors");
user.toLowerCase();
if(comp == 0)
comp2 = "rock";
else if(comp == 1)
comp2 = "paper";
else
comp2 = "scissors";
//Computer wins
if(comp2 == "rock" && user == "scissors")
winner = "The computer wins";
else if(comp2 == "paper" && user == "rock")
winner = "The computer wins";
else if(comp2 == "scissors" && user == "paper")
winner = "The computer wins";
//Tie game
else if(comp2 == "rock" && user == "rock")
winner = "It's a tie";
else if(comp2 == "paper" && user == "paper")
winner = "It's a tie";
else if(comp2 == "scissors" && user == "scissors")
winner = "It's a tie";
//User wins
else if(comp2 == "scissors" && user == "rock")
winner = "You win!";
else if(comp2 == "rock" && user == "paper")
winner = "You win!";
else if(comp2 == "paper" && user == "scissors")
winner = "You win!";
else
winner = "Enter a valid choice";
JOptionPane.showMessageDialog(null, "You picked " + user + "\n" +
"The computer picked " + comp2 + "\n" +
winner);
}
}