Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: In the game Rock Paper Scissors, two players simultaneously choose one of

ID: 3932766 • Letter: J

Question

JAVA:

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.

Explanation / Answer

final static int ROCK = 1, SCISSOR = 2, PAPER = 3;

    public static void main(String[] args)

    {

        Scanner scan = new Scanner(System.in);

        System.out.println("Player 1: Choose (1) - Rock, (2) - Scissors, or (3) - Paper: ");

        int player1 = scan.nextInt();

        System.out.println("Player 2: Choose (1) - Rock, (2) - Scissors, or (3) - Paper: ");

        int player2 = scan.nextInt();

    

        if (player1 == player2)

        {

            System.out.print("It is a tie");

        } else {

            switch (player1){

            case ROCK:

                if (player2 == SCISSOR)

                    System.out.print("Player 1 wins!");

                else

                    System.out.print("Player 2 wins!");

                break;

            case SCISSOR:

                if (player2 == PAPER)

                    System.out.print("Player 1 wins!");

                else

                    System.out.print("Player 2 wins!");

                break;

            case PAPER:

                if (player2 == ROCK)

                    System.out.print("Player 1 wins!");

                else

                    System.out.print("Player 2 wins!");

                break;

            }

        }

    }