我知道我的文档并不出色。
该程序用于创建石头剪刀布游戏:
我最初很难让案例陈述与案例陈述一起正常工作,但我觉得我解决了这个问题。
我的问题:这是在 switch 语句中嵌套 switch 语句的可接受的实现吗?否则,我可以在 userChoice Switch 语句中放入 if 语句。
public static void main(String[] args) {
// Variable declarations
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int sentinel = 4;
int userChoice;
int computerChoice;
// Display Rock, Paper, Scissors menu
menu();
// Obtain user's choice
userChoice = validOption("\nPlease make a selection: ", 1, 4);
// Display game results
while (userChoice != sentinel) {
switch (userChoice) {
case 1:
// Generate computer's choice
computerChoice = rand.nextInt(3) + 1;
System.out.println("Computer's choice: " + computerChoice);
// Determine outcome of the round
switch (computerChoice) {
case 1:
System.out.println("Rock cannot defeat Rock. Draw.");
break;
case 2:
System.out.println("Paper covers Rock. Computer wins.");
break;
case 3:
System.out.println("Rock smashes Scissors. You win!");
break;
}
// Display menu selection and obtain user choice
menu();
userChoice = validOption("\nPlease make a selection: ", 1, 4);
break;
case 2:
// Generate computer's choice
computerChoice = rand.nextInt(3) + 1;
System.out.println("Computer's choice: " + computerChoice);
// Determine outcome of the round
switch (computerChoice) {
case 1:
System.out.println("Paper covers Rock. You win!");
break;
case 2:
System.out.println("Paper cannot defeat Paper. Draw.");
break;
case 3:
System.out.println("Scissors cut Paper. Computer wins.");
break;
}
//Display menu selection and obtain user choice
menu();
userChoice = validOption("\nPlease make a selection: ", 1, 4);
break;
case 3:
// Generate computer's choice
computerChoice = rand.nextInt(3) + 1;
System.out.println("Computer's choice: " + computerChoice);
// Determine outcome of the round
switch (computerChoice) {
case 1:
System.out.println("Rock smashes Scissors. Computer wins.");
break;
case 2:
System.out.println("Scissors cut Paper. You win!");
break;
case 3:
System.out.println("Scissors cannot defeat Scissors. Draw.");
break;
}
// Display menu selection and obtain user choice
menu();
userChoice = validOption("\nPlease make a selection: ", 1, 4);
break;
}
}
System.out.println("Game Over.");
}
// Create menu method
public static void menu () {
System.out.println("\n1 = Rock");
System.out.println("2 = Paper");
System.out.println("3 = Scissors");
System.out.println("4 = End Game\n");
}
/**
* Protects option input from incorrect value (non-numeric, too high or too low)
* @param prompt
* @param minValue
* @param maxValue
* @return
*/
public static int validOption (String prompt,
int minValue,
int maxValue) {
Scanner keyboard = new Scanner (System.in);
int value;
String errorMessage = "Incorrect value. Please select options "
+ "1, 2, 3 or 4\n";
do {
System.out.print(prompt);
if (keyboard.hasNextInt()) {
value = keyboard.nextInt();
if (value < minValue || value > maxValue) {
System.out.println(errorMessage);
} else {
break; // Exit loop.
}
} else {
System.out.println(errorMessage);
}
keyboard.nextLine(); // Clears buffer.
} while (true);
return value;
}
}
最佳答案
恕我直言,只要可读,在 switch 语句内编写 switch 语句本身就可以。为了使代码更具可读性,您可以使用常量而不是 1、2、3 来引用选项:
final int ROCK = 1;
final int PAPER = 1;
final it SCISSORS = 1;
您的开关可能如下所示:
switch (computerChoice) {
case ROCK:
System.out.println("Rock cannot defeat Rock. Draw.");
break;
case PAPER:
System.out.println("Paper covers Rock. Computer wins.");
break;
case SCISSORS:
System.out.println("Rock smashes Scissors. You win!");
break;
}
您可以改进的另一件事是将这两个部分移出外部开关:
// Generate computer's choice
computerChoice = rand.nextInt(3) + 1;
System.out.println("Computer's choice: " + computerChoice);
// and
menu();
userChoice = validOption("\nPlease make a selection: ", 1, 4);
因为您在每种情况下都这样做。
您还可以将内部 switch 语句提取到如下方法中:
private static handlePlayerRock() {
// add the inner switch for the case ROCK of the outer switch here
}
private static handlePlayerScissors() {
// add the inner switch for the case SCISSORS of the outer switch here
}
private static handlePlayerPaper() {
// add the inner switch for the case PAPER of the outer switch here
}
您的开关将如下所示:
switch (userChoice) {
case ROCK: handlePlayerRock();
case PAPER: handlePlayerPaper();
case SCISSORS: handlePlayerScissors();
}
除了嵌套开关之外,还有更高级的方法可以解决此问题。你可以把所有的可能性都放进一个 HashMap<Combination, String>
,其中Combination
是代表玩家和计算机选择的类。该 map 将包含以下内容:
(ROCK, ROCK) -> "Rock cannot defeat Rock. Draw."
(ROCK, PAPER) -> "Paper covers Rock. Computer wins."
...
您可以使用计算机和玩家的选择来访问与 key 关联的值。
关于java - 在 switch 语句中使用 switch 语句是否可以接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49710587/