学习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!");
}
}
}
}