*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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education