明らかにJavaを学んでいます。ゲームをうまく動かすことができました。しかし、ユーザーが R/P/S 以外のものを入力すると、デフォルトで Rock になるようにする必要があります。ループする必要はありません。岩を置くとゲームは完璧に機能します。RPS 以外のものを入れても完璧に動作し、デフォルトでロックします。ただし、紙またははさみを実行すると、紙とはさみの答えが得られ、デフォルトで岩の答えが得られます。これが機能しない理由についてのヒントはありますか?
また、私の方向は間違っていますか?私はこれをより良い方法で行うことができますか?これは本当に...せいぜいエレガントではありません。
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String userChoice="", userInput, compChoice="";
int ranInt = (int)(Math.random()*3);
if (ranInt == 0){
compChoice = "Rock";
} else if (ranInt == 1){
compChoice = "Paper";
}else if (ranInt == 2){
compChoice = "Scissors";
}
System.out.println("Please select one of [R/P/S]: ");
userInput = in.next();
if(userInput.equalsIgnoreCase("p")){
userChoice = "Paper ";
if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(userChoice + "beats "+compChoice +"- you win!");
}else if (compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println("A tie!");
}else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + userChoice + " - i win!");
}
}
if(userInput.equalsIgnoreCase("S")){
userChoice = "Scissors ";
if(compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: "+userChoice);
System.out.println("I choce: "+compChoice);
System.out.println(userChoice + "beats "+compChoice +"- you win!");
}else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println("A tie!");
}else if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + userChoice + " - i win!");
}
}
if(userInput.equalsIgnoreCase("R")){
userChoice = "Rock ";
if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println("A Tie!");
}else if(compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + userChoice + " - you lose!");
}else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: "+userChoice);
System.out.println("I chose: "+compChoice);
System.out.println(userChoice + " beats " + compChoice + " - you win!");
}
}
else {
userChoice = "";
System.out.println("Invalid selection, defaulting to rock.");
if(compChoice.equalsIgnoreCase("Rock")){
System.out.println("You chose: Rock");
System.out.println("I chose: "+compChoice);
System.out.println("A Tie!");
}else if (compChoice.equalsIgnoreCase("Paper")){
System.out.println("You chose: Rock");
System.out.println("I chose: "+compChoice);
System.out.println(compChoice + " beats " + "Rock - you lose!");
} else if(compChoice.equalsIgnoreCase("Scissors")){
System.out.println("You chose: Rock");
System.out.println("I chose: "+compChoice);
System.out.println("Rock beats " + compChoice + " - you win!");
}
}
}
}