尝试创建一个摇滚纸剪刀游戏,但在获得用户选择之后,随机生成计算机选择并将其显示在程序中。该程序没有进入确定获胜者的下一组代码。
Scanner in = new Scanner(System.in);
int ComputerChoice = (int)(3 * Math.random()) + 1;
System.out.println("Please select one of the following [R/P/S]: ");
String UserChoice = in.nextLine();
if (UserChoice.equalsIgnoreCase("R") ) {
System.out.println("You chose: Rock"); }
else if (UserChoice.equalsIgnoreCase("P") ) {
System.out.println("You chose: Paper"); }
else if (UserChoice.equalsIgnoreCase("S") ) {
System.out.println("You chose: Scissors"); }
else if (UserChoice != "R" || UserChoice != "S" || UserChoice != "P") {
UserChoice = "R";
System.out.println("Invalid choice! Defaulting to Rock.");
}
if (ComputerChoice == 1) {
System.out.println("I chose: Rock");
}
else if (ComputerChoice == 2) {
System.out.println("I chose: Paper");
}
else if (ComputerChoice == 3) {
System.out.println("I chose: Scissors");
}
if (UserChoice == "R" && ComputerChoice == 1) {
System.out.println("A Tie!");
}
else if (UserChoice == "R" && ComputerChoice == 2) {
System.out.println("Paper beats rock - you lose!");
}
else if (UserChoice == "R" && ComputerChoice == 3) {
System.out.println("Rock beats scissors - you win!");
}
else if (UserChoice == "P" && ComputerChoice == 2) {
System.out.println("A Tie!");
}
else if (UserChoice == "P" && ComputerChoice == 1) {
System.out.println("Paper beats rock - you win!");
}
else if (UserChoice == "P" && ComputerChoice == 3) {
System.out.println("Scissors beats paper - you lose!");
}
else if (UserChoice == "S" && ComputerChoice == 1) {
System.out.println("Rock beats scissors - you lose!");
}
else if (UserChoice == "S" && ComputerChoice == 3) {
System.out.println("A Tie!");
}
else if (UserChoice == "s" && ComputerChoice == 2) {
System.out.println("Scissors beats paper - you win!");