Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 3 Exercise 17, Introduction to Java Programming, Tenth Edition Y. Daniel

ID: 3780925 • Letter: C

Question

Chapter 3 Exercise 17, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.
*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. Here are sample runs:
scissor (0), rock (1), paper (2): 1
The computer is scissor. You are rock. You won
scissor (0), rock (1), paper (2): 2
The computer is paper. You are paper too. It is a draw.

Explanation / Answer

class Scissor-rock-paper

{

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int computer, user;

System.out.print("scissor (0), rock (1), paper (2):");
user = s.nextInt();
computer = (int)(Math.random() * 3);

if(computer == 0) {
System.out.print(" computer is scissor.");
} else if(computer == 1) {
System.out.print("computer is rock.");
} else if(computer == 2) {
   System.out.print(" computer is paper.");
}

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.");
}
}
}}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote