我知道我的文档不是很棒。
这个程序是创建一个Rock,Paper,Scissors Game:
我最初努力让案例陈述与案例陈述正常工作,但我觉得我解决了这个问题。
我的问题:这是在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;
}
}
答案 0 :(得分:2)
恕我直言,只要它是可读的,在switch语句中编写switch语句本身就是好的。为了使代码更具可读性,您可以使用常量而不是1,2,3来引用选项:
[101100, 'hello']
[101101, 'twenty']
您的开关可能如下所示:
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;
}
因为你在每种情况下都这样做。
您还可以将内部switch语句解压缩为如下方法:
// 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);
您的开关将如下所示:
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
您可以使用计算机和玩家的选择来访问与密钥相关联的值。
答案 1 :(得分:0)
是的,您对嵌套switch语句的实现是正确的。 break语句在嵌套的switch语句中至关重要,因为它告诉Java程序找到了正确的选择并且程序可以继续而不必执行switch case语句中列出的其他选项,并且已经放置了所有的break声明正确。希望这很有帮助。