Question: Scanner scanner = new Scanner ( System . in ) ; String playAgain = yes; / / Game loop: Continue asking to play again as

Scanner scanner = new Scanner(System.in); String playAgain = "yes"; // Game loop: Continue asking to play again as long as the user answers yes or a variation. while (playAgain.equalsIgnoreCase("yes")|| playAgain.equalsIgnoreCase("y")){// Get Player 1's choice int player1Choice = getPlayerChoice(scanner,1); // Get Player 2's choice int player2Choice = getPlayerChoice(scanner,2); // Determine the winner based on the choices String result = determineWinner(player1Choice, player2Choice); // Output the result System.out.println(result); // Ask if the players want to play again System.out.print("Would you like to play again? "); playAgain = scanner.nextLine(); } System.out.println("Thanks for playing!"); scanner.close(); }// Method to prompt the player for their choice and validate the input public static int getPlayerChoice(Scanner scanner, int playerNumber){ int choice =0; boolean validChoice = false; // Continue asking until a valid choice (1-5) is entered while (!validChoice){ System.out.print("Player "+ playerNumber +" enter a number 1-5 for Gorilla, Terminator, Gangster, Warrior, or Zombie: "); if (scanner.hasNextInt()){ choice = scanner.nextInt(); if (choice >=1 && choice <=5){ validChoice = true; } else { System.out.println("Invalid choice, Player "+ playerNumber +". Enter a number 1-5: "); }} else { System.out.println("Invalid input. Please enter a valid number."); scanner.next(); // Clear invalid input }} scanner.nextLine(); // Clear the newline left by nextInt() return choice; }// Method to determine the winner based on player choices public static String determineWinner(int player1Choice, int player2Choice){// Check if it's a tie if (player1Choice == player2Choice){ return "Nobody wins"; }// Define the win conditions if ((player1Choice ==1 && player2Choice ==4)||// Gorilla fools Warrior (player1Choice ==1 && player2Choice ==2)||// Gorilla unplugs Terminator (player1Choice ==2 && player2Choice ==4)||// Terminator chokes Warrior (player1Choice ==2 && player2Choice ==5)||// Terminator crushes Zombie (player1Choice ==3 && player2Choice ==2)||// Gangster drowns Terminator (player1Choice ==3 && player2Choice ==1)||// Gangster skewers Gorilla (player1Choice ==4 && player2Choice ==3)||// Warrior chops Gangster (player1Choice ==4 && player2Choice ==5)||// Warrior decapitates Zombie (player1Choice ==5 && player2Choice ==3)||// Zombie eats Gangster (player1Choice ==5 && player2Choice ==1)){// Zombie savages Gorilla return "Player 1 wins!"; }// If none of the above conditions match, Player 2 wins return "Player 2 wins!"; }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!