IN JAVA: Scissor, Rock, or Paper computer game simulation • Import Scanner • Gen
ID: 673845 • Letter: I
Question
IN JAVA: Scissor, Rock, or Paper computer game simulation
• Import Scanner
• Generate a random number (between 0 – 2) by using the Math.random() method and assign it to a variable called “computerNumber” (decide whether it should be an int or a double?)
• Display: “scissor (0), rock (1), paper (2):” via the System.out.print();
• Enter a number (between 0 and 2, inclusively) via Scanner and assign it to “userInput” variable (should it be int? double?)
• Display the results Hints:
• Use a Switch statement to test what number is inside the computerNumber • case 0 • If a user enters a “0”, then print “The computer is scissor. You are scissor too. It is a draw” • If a user enters a “1”, then print: "The computer is scissor. You are rock. You won" • If a user enters a “2”, then print: "The computer is scissor. You are paper. You lost" • Similarly, proceed with the other two cases: • In the next case, the computer is a “rock” and the user is scissor/rock/paper • In the final case, the computer is a “paper” and the user is scissor/rock/paper • Use “else if” statements inside the “switch” for user input and break out of the switch block if/when a condition is satisfied
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class ScissorRockPaper
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int computerNumber = (int)(Math.random() * 3);
System.out.print("Scissor (0), Rock (1), Paper (2): ");
int userInput = in.nextInt();
switch(computerNumber)
{
case 0:
System.out.print("The computer is Scissor. ");
if(userInput == 0)
System.out.println("You are Scissor too. It is a draw.");
else if(userInput == 1)
System.out.println("You are a Rock. You won.");
else
System.out.println("You are a Paper. You lost");
break;
case 1:
System.out.println("The computer is Rock. ");
if(userInput == 0)
System.out.println("You are Scissor. You lost.");
else if(userInput == 1)
System.out.println("You are a Rock too. Its a draw.");
else
System.out.println("You are a Paper. You won.");
break;
case 2:
System.out.println("The computer is Paper. ");
if(userInput == 0)
System.out.println("You are Scissor. You won.");
else if(userInput == 1)
System.out.println("You are a Rock. You lost.");
else
System.out.println("You are a Paper too. Its a draw.");
break;
}
}
}