Question: Scanner scanner = new Scanner ( System . in ) ; String playAgain = yes; / / Game loop: Continue asking to play again as
Scanner scanner new ScannerSystemin; String playAgain "yes"; Game loop: Continue asking to play again as long as the user answers yes or a variation. while playAgainequalsIgnoreCaseyes playAgain.equalsIgnoreCasey Get Player s choice int playerChoice getPlayerChoicescanner; Get Player s choice int playerChoice getPlayerChoicescanner; Determine the winner based on the choices String result determineWinnerplayerChoice, playerChoice; Output the result System.out.printlnresult; Ask if the players want to play again System.out.printWould you like to play again? ; playAgain scanner.nextLine; System.out.printlnThanks for playing!"; scanner.close; Method to prompt the player for their choice and validate the input public static int getPlayerChoiceScanner scanner, int playerNumber int choice ; boolean validChoice false; Continue asking until a valid choice is entered while validChoice System.out.printPlayer playerNumber enter a number for Gorilla, Terminator, Gangster, Warrior, or Zombie: ; if scannerhasNextInt choice scanner.nextInt; if choice && choice validChoice true; else System.out.printlnInvalid choice, Player playerNumber Enter a number : ; else System.out.printlnInvalid 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 determineWinnerint playerChoice, int playerChoice Check if it's a tie if playerChoice playerChoice return "Nobody wins"; Define the win conditions if playerChoice && playerChoice Gorilla fools Warrior playerChoice && playerChoice Gorilla unplugs Terminator playerChoice && playerChoice Terminator chokes Warrior playerChoice && playerChoice Terminator crushes Zombie playerChoice && playerChoice Gangster drowns Terminator playerChoice && playerChoice Gangster skewers Gorilla playerChoice && playerChoice Warrior chops Gangster playerChoice && playerChoice Warrior decapitates Zombie playerChoice && playerChoice Zombie eats Gangster playerChoice && playerChoice Zombie savages Gorilla return "Player wins!"; If none of the above conditions match, Player wins return "Player wins!";
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
