I know my documentation is not superb.
This program is to create a Rock, Paper, Scissors Game:
I initially struggled with getting the case statements withing case statements to work properly but I feel like I resolved the issue.
My question: is this an acceptable implementation of nesting a switch statement within a switch statement. Otherwise, I could put an if statement within the userChoice Switch statement instead.
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;
}
}