Java 在switch语句中使用switch语句是否可以接受?
我知道我的文档不是很好 本程序旨在创建一个石头、布、剪刀游戏: 起初,我努力让案例陈述和案例陈述正常工作,但我觉得我解决了这个问题 我的问题是:这是在switch语句中嵌套switch语句的可接受实现吗。否则,我可以将if语句放在userChoice开关语句中Java 在switch语句中使用switch语句是否可以接受?,java,while-loop,switch-statement,case-statement,Java,While Loop,Switch Statement,Case Statement,我知道我的文档不是很好 本程序旨在创建一个石头、布、剪刀游戏: 起初,我努力让案例陈述和案例陈述正常工作,但我觉得我解决了这个问题 我的问题是:这是在switch语句中嵌套switch语句的可接受实现吗。否则,我可以将if语句放在userChoice开关语句中 public static void main(String[] args) { // Variable declarations Scanner keyboard = new Scanner(System.in);
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;
}
publicstaticvoidmain(字符串[]args){
//变量声明
扫描仪键盘=新扫描仪(System.in);
Random rand=新的Random();
int-sentinel=4;
int用户选择;
国际计算机选择;
//显示石头、布、剪刀菜单
菜单();
//获得用户的选择
userChoice=validOption(“\n请选择:”,1,4);
//显示比赛结果
while(userChoice!=哨兵){
开关(用户选择){
案例1:
//生成计算机的选择
computerChoice=rand.nextInt(3)+1;
System.out.println(“计算机的选择:“+computerChoice”);
//确定回合结果
开关(计算机选择){
案例1:
System.out.println(“岩石无法击败岩石。绘制”);
打破
案例2:
System.out.println(“纸张覆盖岩石,计算机获胜”);
打破
案例3:
System.out.println(“石头砸剪刀,你赢了!”);
打破
}
//显示菜单选择并获得用户选择
菜单();
userChoice=validOption(“\n请选择:”,1,4);
打破
案例2:
//生成计算机的选择
computerChoice=rand.nextInt(3)+1;
System.out.println(“计算机的选择:“+computerChoice”);
//确定回合结果
开关(计算机选择){
案例1:
System.out.println(“纸张覆盖岩石,你赢了!”);
打破
案例2:
System.out.println(“纸不能打败纸.画”);
打破
案例3:
System.out.println(“剪刀剪纸,电脑赢”);
打破
}
//显示菜单选择并获得用户选择
菜单();
userChoice=validOption(“\n请选择:”,1,4);
打破
案例3:
//生成计算机的选择
computerChoice=rand.nextInt(3)+1;
System.out.println(“计算机的选择:“+computerChoice”);
//确定回合结果
开关(计算机选择){
案例1:
System.out.println(“石头砸剪刀,电脑赢”);
打破
案例2:
System.out.println(“剪刀剪纸,你赢了!”);
打破
案例3:
System.out.println(“剪刀不能打败剪刀。画”);
打破
}
//显示菜单选择并获得用户选择
菜单();
userChoice=validOption(“\n请选择:”,1,4);
打破
}
}
System.out.println(“游戏结束”);
}
//创建菜单方法
公共静态无效菜单(){
System.out.println(“\n1=Rock”);
System.out.println(“2=纸张”);
System.out.println(“3=剪刀”);
System.out.println(“4=游戏结束\n”);
}
/**
*防止选项输入值不正确(非数字、过高或过低)
*@param提示符
*@param minValue
*@param maxValue
*@返回
*/
公共静态整数有效性(字符串提示,
int minValue,
整数(最大值){
扫描仪键盘=新扫描仪(System.in);
int值;
String errorMessage=“值不正确。请选择选项”
+“1、2、3或4\n”;
做{
系统输出打印(提示);
if(keyboard.hasNextInt()){
value=keyboard.nextInt();
如果(值<最小值| |值>最大值){
System.out.println(错误消息);
}否则{
break;//退出循环。
}
}否则{
System.out.println(错误消息);
}
keyboard.nextLine();//清除缓冲区。
}虽然(正确);
返回值;
}
}是的,嵌套switch语句的实现是正确的。break语句在嵌套switch语句中非常重要,因为它告诉Java程序已找到正确的选择,并且程序可以继续,而不必执行switch case语句中列出的其他选择,并且您已正确放置了所有break语句。希望这会有所帮助。IMHO,只要它是可读的,在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;
}
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();
}
您可以改进的另一件事是将这两个部件移出外部开关:
// 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语句提取到如下方法中:
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;
}
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();
}
然后,您的交换机将如下所示:
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;
}
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
,w