うまくいけば、これは簡単な質問です。シンプルなジャンケンゲームを作っています。私はランダムなコンピューターの選択をうまく行い、ユーザーの選択をうまくさせます。しかし、誰が勝つかを見つけようとすると、無効な入力用のelse ifブロックの最後のelseが出力されます。
正しい選択が行われると、「有効な選択を入力してください」と出力されます。
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);
}
}