我正在尝试制作一个摇滚纸剪刀 - 蜥蜴游戏。我一直在学习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.");
}
}
}
答案 0 :(得分:1)
不要只是要求玩家1选择一次,而是将其放入while循环中询问,直到他们输入有效的响应,如此
player1Choice = "";
boolean player1ChoiceValid = false;
while(!player1ChoiceValid){
System.out.println(player1 + ", please enter your command:");
player1Choice = kb.nextLine();
if(player1Choice.equals("1")
|| player1Choice.equals("2")
|| player1Choice.equals("3")
|| player1Choice.equals("4")
|| player1Choice.equals("5")){
player1ChoiceValid = true;
}else{
System.out.println("I'm sorry, this is not a valid command.");
}
}
这将持续循环问题直到输入有效,这意味着它是1或2或3或4或5.这也应该对玩家2进行。
答案 1 :(得分:0)
while (true) {
Scanner kb = new Scanner(System.in);
System.out.println(player1 + ", please enter your command:");
String player1Choice = kb.nextLine();
if (!player1Choice.equals("1") || !player1Choice.equals("2") || !player1Choice.equals("3")
|| !player1Choice.equals("4") || !player1.equals("5")) {
System.out.println("Sorry, this is not a valid command");
} else {
break;
}
}
此外,您可以在if语句中考虑更优雅的方法。