我正在試圖製作一款石頭剪刀sp l蜥蜴遊戲。我一直在學習if/else語句。我試圖做到這一點,如果player1輸入一個無效的命令,那麼它會打印出「對不起,這不是一個有效的命令」像else語句。如果兩個玩家輸入一個命令並且一個是錯誤的,但它不起作用,但是如果玩家1沒有輸入命令則可以。你能幫我弄清楚我應該做什麼嗎? 這裏是我的代碼:我該如何做到這一點,如果第一個玩家輸入錯誤的命令,那麼它會有一個特定的提示?
package csc212hw03;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String player1;
String player2;
String player1Choice;
String player2Choice;
String line;
// 「1」 for Paper
//「2」 for Rock
//「3」 for Spock
//「4」 for Lizard
//「5」for Scissors
Scanner kb = new Scanner(System.in);
System.out.println("Player 1, please enter your name:");
player1 = kb.nextLine();
System.out.println("Player 2, please enter your name:");
player2 = kb.nextLine();
System.out.println(player1 + ", please enter your command:");
player1Choice = kb.nextLine();
System.out.println(player2 + ", please enter your command:");
player2Choice = kb.nextLine();
if (player1Choice.equals("1") && player2Choice.equals("2")) {
System.out.println(player1 + " wins! Paper covers Rock.");
System.out.println("Thank you for playing.");
} else if (player2Choice.equals("2") && player1Choice.equals("1")){
System.out.println(player2 + " wins! Paper covers Rock.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("1")&& player2Choice.equals("1")) {
System.out.println("Draw!");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("2")&& player2Choice.equals("2")) {
System.out.println("Draw!");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("1")&& player2Choice.equals("3")) {
System.out.println(player1 + " wins! Paper disproves Spock.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("3") && player2Choice.equals("1")) {
System.out.println(player2 + " wins! Paper disproves Spock.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("3")&& player2Choice.equals("3")) {
System.out.println("Draw!");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("2") && player2Choice.equals("3")) {
System.out.println(player2 + " wins! Spock vaporizes Rock.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("3") && player2Choice.equals("2")) {
System.out.println(player1 + " wins! Spock vaporizes Rock.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("3") && player2Choice.equals("5")) {
System.out.println(player1 + " wins! Spock smashes Scissors.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("5") && player2Choice.equals("3")) {
System.out.println(player2 + " wins! Spock smashes Scissors.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("5") && player2Choice.equals("5")) {
System.out.println("Draw!");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("2") && player2Choice.equals("5")) {
System.out.println(player1 + " wins! Rock crushes Scissors.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("5") && player2Choice.equals("2")) {
System.out.println(player2 + " wins! Rock crushes Scissors.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("2") && player2Choice.equals("4")) {
System.out.println(player1 + " wins! Rock crushes Lizard.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("4") && player2Choice.equals("2")) {
System.out.println(player2 + " wins! Rock crushes Lizard.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("3") && player2Choice.equals("4")) {
System.out.println(player2 + " wins! Lizard poisons Spock.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("4") && player2Choice.equals("3")) {
System.out.println(player1 + " wins! Lizard poisons Spock.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("5") && player2Choice.equals("1")) {
System.out.println(player1 + " wins! Scissors cuts Paper.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("1") && player2Choice.equals("5")) {
System.out.println(player2 + " wins! Scissors cuts Paper.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("5") && player2Choice.equals("4")) {
System.out.println(player1 + " wins! Scissors decpitates Lizard.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("4") && player2Choice.equals("5")) {
System.out.println(player2 + " wins! Scissors decapitates Lizard.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("5") && player2Choice.equals("5")) {
System.out.println("Draw!");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("4") && player2Choice.equals("4")) {
System.out.println("Draw!");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("4") && player2Choice.equals("1")) {
System.out.println(player1 + " wins! Lizard eats Paper.");
System.out.println("Thank you for playing.");
} else if (player1Choice.equals("1") && player2Choice.equals("4")) {
System.out.println(player2 + " wins! Lizard eats Paper.");
System.out.println("Thank you for playing.");
} else {
System.out.println("I'm sorry, this is not a valid command.");
System.out.println("Thank you for playing.");
}
}
}