*3.17 (Game: scissor, rock, paper) Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.

CMPTR
3rd Edition
ISBN:9781337681872
Author:PINARD
Publisher:PINARD
Chapter4: Computer Networks
Section: Chapter Questions
Problem 12QY
icon
Related questions
Question
Java Use the Random Class and the nextInt() method to generate random numbers in the range of 0 - 2. Use a switch statement to match the number generated for the computer that represents either scissor, rock or paper. Then use a multi way if-else statement to determine whether the computer wins, loses or there is a draw. Make sure you output a statement indicating what the computer represents and what the user represents and who won. It should look like the output given in the text.
T-Mobile Wi-Fi
<
OO
O
1:31 PM
OOL
00
*3.17 (Game: scissor, rock, paper)
Write a program that plays the
popular scissor-rock-paper game. (A
scissor can cut a paper, a rock can
knock a scissor, and a paper can wrap
a rock.) The program randomly
generates a number 0, 1, or 2
representing scissor, rock, and paper.
The program prompts the user to
enter a number 0, 1, or 2 and displays
a message indicating whether the user
or the computer wins, loses, or draws.
180
@ 7% (
Q
= र ||
AA
|||
=
Transcribed Image Text:T-Mobile Wi-Fi < OO O 1:31 PM OOL 00 *3.17 (Game: scissor, rock, paper) Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. 180 @ 7% ( Q = र || AA ||| =
Expert Solution
Step 1

The source code of the program

import java.util.Random;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Random rand = new Random();
        Scanner sc = new Scanner(System.in);
        int computerChoice = rand.nextInt(3); // 0 = scissors, 1 = rock, 2 = paper
        System.out.print("Enter your choice (0 = scissors, 1 = rock, 2 = paper): ");
        int userChoice = sc.nextInt();
        String computerChoiceStr = "";
        switch (computerChoice) {
            case 0:
                computerChoiceStr = "scissors";
                break;
            case 1:
                computerChoiceStr = "rock";
                break;
            case 2:
                computerChoiceStr = "paper";
                break;
        }
        String userChoiceStr = "";
        switch (userChoice) {
            case 0:
                userChoiceStr = "scissors";
                break;
            case 1:
                userChoiceStr = "rock";
                break;
            case 2:
                userChoiceStr = "paper";
                break;
        }
        if (computerChoice == userChoice) {
            System.out.println("Computer chose " + computerChoiceStr 
            + ", User chose " + userChoiceStr + ", it's a draw!");
        } else if (computerChoice == 0 && userChoice == 2 ||
                   computerChoice == 1 && userChoice == 0 ||
                   computerChoice == 2 && userChoice == 1) {
            System.out.println("Computer chose " + computerChoiceStr 
            + ", User chose " + userChoiceStr + ", computer wins!");
        } else {
            System.out.println("Computer chose " + computerChoiceStr 
            + ", User chose " + userChoiceStr + ", user wins!");
        }
    }
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer