Reputation: 21
The user has to input 2 variables at once in order to get the outputted screen. Any help on the code?
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!");
}
Upvotes: 1
Views: 41
Reputation: 4592
If you must do it with a switch
, there a couple of ways you might go about it. The trick is that case
labels can only be single values, so you must somehow combine the two inputs into a single value that that can be matched by a case
label.
If you only need a two-way test (e.g., user choices were either 1 and 5, or something else), reducing the inputs to a yes/no answer is sufficient. You could do something like this:
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!");
}
If it's going to be a true switch
with multiple possible cases, you'll need to get a bit more creative. You could, for instance, create a String
containing the user's choices in a predictable format that you can then match with a case
. For example:
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!");
}
Yet another way would be to combine the user's choices into a single number, in a way that preserves both values. For example, let's say we know that valid choices for either input will be less than 10. We could use:
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!");
}
Upvotes: 1