.3.17 (Game: scissor, rock, paper write a program that plays the popular scissor
ID: 3795476 • Letter: #
Question
.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 e, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number e, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, draws. is a or Here sample run sample Run for Exercise03 17 Enter input data for the propam ample data pr below. You may modify it) Show the sample output Using the Preceeding input ikonmand Java Exercisea3 17 scissor (a), rock (1 paper (2 The computer is paper. You are paper too. It is a draw commandExplanation / Answer
Program:
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int computer, user;
//Prompts User For Inputting The Number
System.out.print("scissor (0), rock (1), paper (2):");
user = s.nextInt();
computer = (int)(Math.random() * 3);
if(computer == 0) {
System.out.print("The computer is scissor.");
} else if(computer == 1) {
System.out.print("The computer is rock.");
} else if(computer == 2) {
System.out.print("The computer is paper.");
}
//Checking out Who is the winner?
if((user == 0 && computer == 2) || (user == 1 && computer == 0) || (user == 2 && computer == 1)) {
if(user == 0) {
System.out.print(" You are scissor. You won.");
} else if(user == 1) {
System.out.print(" You are rock. You won.");
} else if(user == 2) {
System.out.print(" You are paper. You won.");
}
} else if(user == computer) {
if(user == 0) {
System.out.print(" You are scissor too. It is a draw.");
} else if(user == 1) {
System.out.print(" You are rock too. It is a draw.");
} else if(user == 2) {
System.out.print(" You are paper too. It is a draw.");
}
} else {
if(user == 0) {
System.out.print(" You are scissor. You lose.");
} else if(user == 1) {
System.out.print(" You are rock. You lose.");
} else if(user == 2) {
System.out.print(" You are paper. You lose.");
}
}
}
}
Output:
scissor (0), rock (1), paper (2):1
The computer is paper. You are rock. You lose.
scissor (0), rock (1), paper (2):2
The computer is scissor. You are paper. You lose.
scissor (0), rock (1), paper (2):0
The computer is scissor. You are scissor too. It is a draw.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.