当用户输入中间带有 'and' 的 2 个值时,如何执行一个案例?
How do I make it so when the user inputs 2 values with 'and' in the middle it executes one case?
用户必须一次输入 2 个变量才能获得输出屏幕。对代码有帮助吗?
switch(cardChoice)
{
case 1 && 5:
System.out.println("You have matched the :) card! You get 10 Points!");
System.out.println("------- ------- ------- ------- ------- -------");
System.out.println("| | | | | | | | | | | |");
System.out.println("| :) | | 2 | | 3 | | 4 | | :) | | 6 |");
System.out.println("| | | | | | | | | | | |");
System.out.println("------- ------- ------- ------- ------- -------");
System.out.println("------- ------- ------- ------- ------- -------");
System.out.println("| | | | | | | | | | | |");
System.out.println("| 7 | | 8 | | 9 | | 10 | | 11 | | 12 |");
System.out.println("| | | | | | | | | | | |");
System.out.println("------- ------- ------- ------- ------- -------");
cardPoints = cardPoints + 10;
break;
default:
System.out.println("Invalid Input!");
}
如果您必须使用 switch
进行操作,您可以采用多种方法。诀窍在于 case
标签只能是单个值,因此您必须以某种方式将两个输入组合成一个可以与 case
标签匹配的值。
如果您只需要双向测试(例如,用户选择 1 和 5,或其他),将输入减少为 yes/no 答案就足够了。你可以这样做:
int choice1, choice2;
System.out.println("Enter the number of your first card choice:");
choice1 = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter the number of your second card choice:");
choice2 = scanner.nextInt();
scanner.nextLine();
// ...
switch (choice1 == 1 && choice2 == 5 ? "yes" : "no"){
case "yes":
// RIGHT!
break;
default:
System.out.println("Invlid input!");
}
如果它要成为一个真实的 switch
有多种可能的情况,你需要更有创意一点。例如,您可以创建一个 String
以可预测的格式包含用户的选择,然后您可以将其与 case
匹配。例如:
int choice1, choice2;
System.out.println("Enter the number of your first card choice:");
choice1 = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter the number of your second card choice:");
choice2 = scanner.nextInt();
scanner.nextLine();
// ...
String userChoice = String.format("%02d,%02d", choice1, choice2);
switch (userChoice){
case "01,05":
// RIGHT!
break;
case "02,04":
// Another right answer!
break;
default:
System.out.println("Invlid input!");
}
另一种方法是将用户的选择组合成一个数字,以保留两个值的方式。例如,假设我们知道任一输入的有效选择都将少于 10。我们可以使用:
int choice1, choice2;
System.out.println("Enter the number of your first card choice:");
choice1 = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter the number of your second card choice:");
choice2 = scanner.nextInt();
scanner.nextLine();
// ...
switch (choice1 * 10 + choice2){
case 15: // User chose 1 and 5
// RIGHT!
break;
case 24: // User chose 2 and 4
// Another right answer!
break;
default:
System.out.println("Invlid input!");
}
用户必须一次输入 2 个变量才能获得输出屏幕。对代码有帮助吗?
switch(cardChoice)
{
case 1 && 5:
System.out.println("You have matched the :) card! You get 10 Points!");
System.out.println("------- ------- ------- ------- ------- -------");
System.out.println("| | | | | | | | | | | |");
System.out.println("| :) | | 2 | | 3 | | 4 | | :) | | 6 |");
System.out.println("| | | | | | | | | | | |");
System.out.println("------- ------- ------- ------- ------- -------");
System.out.println("------- ------- ------- ------- ------- -------");
System.out.println("| | | | | | | | | | | |");
System.out.println("| 7 | | 8 | | 9 | | 10 | | 11 | | 12 |");
System.out.println("| | | | | | | | | | | |");
System.out.println("------- ------- ------- ------- ------- -------");
cardPoints = cardPoints + 10;
break;
default:
System.out.println("Invalid Input!");
}
如果您必须使用 switch
进行操作,您可以采用多种方法。诀窍在于 case
标签只能是单个值,因此您必须以某种方式将两个输入组合成一个可以与 case
标签匹配的值。
如果您只需要双向测试(例如,用户选择 1 和 5,或其他),将输入减少为 yes/no 答案就足够了。你可以这样做:
int choice1, choice2;
System.out.println("Enter the number of your first card choice:");
choice1 = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter the number of your second card choice:");
choice2 = scanner.nextInt();
scanner.nextLine();
// ...
switch (choice1 == 1 && choice2 == 5 ? "yes" : "no"){
case "yes":
// RIGHT!
break;
default:
System.out.println("Invlid input!");
}
如果它要成为一个真实的 switch
有多种可能的情况,你需要更有创意一点。例如,您可以创建一个 String
以可预测的格式包含用户的选择,然后您可以将其与 case
匹配。例如:
int choice1, choice2;
System.out.println("Enter the number of your first card choice:");
choice1 = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter the number of your second card choice:");
choice2 = scanner.nextInt();
scanner.nextLine();
// ...
String userChoice = String.format("%02d,%02d", choice1, choice2);
switch (userChoice){
case "01,05":
// RIGHT!
break;
case "02,04":
// Another right answer!
break;
default:
System.out.println("Invlid input!");
}
另一种方法是将用户的选择组合成一个数字,以保留两个值的方式。例如,假设我们知道任一输入的有效选择都将少于 10。我们可以使用:
int choice1, choice2;
System.out.println("Enter the number of your first card choice:");
choice1 = scanner.nextInt();
scanner.nextLine();
System.out.println("Enter the number of your second card choice:");
choice2 = scanner.nextInt();
scanner.nextLine();
// ...
switch (choice1 * 10 + choice2){
case 15: // User chose 1 and 5
// RIGHT!
break;
case 24: // User chose 2 and 4
// Another right answer!
break;
default:
System.out.println("Invlid input!");
}