206k views
3 votes
Game: scissor, rock, paper. Write a program that plays the 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. Use: 1.The Scanner Class 2.Switch Statement 3.if else if else Statements 4.Math class 5.IntelliJ as the IDE

User Toddams
by
5.1k points

1 Answer

5 votes

Answer:

import java.util.*;

public class Main

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

Random r = new Random();

int computer = r.nextInt(3);

System.out.print("Enter a number[0-1-2]: ");

int user = input.nextInt();

switch(computer){

case 0:

if(user == 0)

System.out.println("It is a draw");

else if(user == 1)

System.out.println("User wins");

else

System.out.println("Computer wins");

break;

case 1:

if(user == 0)

System.out.println("Computer wins");

else if(user == 1)

System.out.println("It is a draw");

else

System.out.println("User wins");

break;

case 2:

if(user == 0)

System.out.println("User wins");

else if(user == 1)

System.out.println("Computer wins");

else

System.out.println("It is a draw");

break;

}

}

}

Step-by-step explanation:

Create objects to use the Scanner and Random class

Using the nextInt() method with Random object r, generate a random number between 0 to 2 and set it to the variable named computer

Ask the user to enter a number using Scanner object and nextInt() method and set it to the variable named user

Check the value of computer variable using switch statement.

When computer is 0, check the value of the user variable using if statement. If user is 0, it is a draw. If user is 1, the user wins. Otherwise, the computer wins.

When computer is 1, check the value of the user variable using if statement. If user is 0, the computer wins. If user is 1, it is a draw. Otherwise, the user wins.

When computer is 2, check the value of the user variable using if statement. If user is 0, the user wins. If user is 1, the computer wins. Otherwise, it is a draw.

User Wjchulme
by
5.7k points