In: Computer Science
In the game Rock Paper Scissors, two players simultaneously choose one of three
options: rock, paper, or scissors. If both players choose the same option, then the
result is a tie. However, if they choose differently, the winner is determined as
follows:
•
Rock beats scissors, because a rock can break a pair of scissors.
•
Scissors beats paper, because scissors can cut paper.
•
Paper beats rock, because a piece of paper can cover a rock.
Create a game in which the computer randomly chooses rock, paper, or scissors.
Let the user enter a number 1, 2, or 3, each representing one of the three choices.
Then, determine the winner. Save the application as
RockPaperScissors.java
.
(In the chapter “Characters, Strings, and the
StringBuilder
,” you will modify the
game so that the user enters a string for
rock, paper
, and
scissors
, rather than just
entering a number.)
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. The question does refers to an existing class but I see it was not shared here, hence I had to write the whole program from scratch. Based on my best of my understanding I have written the program, if anything needs to change or modified please do comment and I will make the necessary changes. Thank You !! =========================================================================== import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { String playerOne, playerTwo; Scanner scanner = new Scanner(System.in); System.out.print("Player 1: Please type Rock, Paper or Scissors: "); playerOne = scanner.nextLine(); System.out.print("Player 2: Please type Rock, Paper or Scissors: "); playerTwo = scanner.nextLine(); printWinner(playerOne, playerTwo); } // method that contains logic to determine the winner // and print the results private static void printWinner(String playerOne, String playerTwo) { if (playerOne.equalsIgnoreCase("rock") && playerTwo.equalsIgnoreCase("scissors")) { System.out.println("Player 1 is the winner!"); } else if (playerOne.equalsIgnoreCase("scissors") && playerTwo.equalsIgnoreCase("paper")) { System.out.println("Player 1 is the winner!"); } else if (playerOne.equalsIgnoreCase("paper") && playerTwo.equalsIgnoreCase("rock")) { System.out.println("Player 1 is the winner!"); } else if (playerOne.toUpperCase().equals(playerTwo.toUpperCase())) { System.out.println("Match Draw!"); } else if (playerTwo.equalsIgnoreCase("rock") && playerOne.equalsIgnoreCase("scissors")) { System.out.println("Player 2 is the winner!"); } else if (playerTwo.equalsIgnoreCase("scissors") && playerOne.equalsIgnoreCase("paper")) { System.out.println("Player 2 is the winner!"); } else if (playerTwo.equalsIgnoreCase("paper") && playerOne.equalsIgnoreCase("rock")) { System.out.println("Player 2 is the winner!"); } else { System.out.println("Invalid inputs!"); } } }
===========================================================================