Write a program that plays the popular scissor-rockpaper 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.
Click here to see other solutions to Introduction to Java Programming.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br /><br />import java.util.Scanner;<br /><br />public class ComputeAndInterpretBMI {<br /> public static void main (String [] args) {<br /><br /> int number, guess;<br /><br /> // Obtain the random number 0 or 1 <br /> number = (int) (Math.random() * 3);<br /> // Prompt the user to enter a guess<br /> Scanner input = new Scanner(System.in);<br /><br /> System.out.print("Guessing Game: Scissor, Rock, Paper."<br /> + "nEnter 0 for Scissor, 1 for Rock or 2 for Paper: ");<br /> guess = input.nextInt();<br /><br /> // Check the guess<br /> if (number == 0 && guess == 0)<br /> System.out.println("The computer is Scissors. You are Scissors too. It is a draw.");<br /> if (number == 0 && guess == 1)<br /> System.out.println("The computer is Scissors. You are Rock. You won.");<br /> if (number == 0 && guess == 2)<br /> System.out.println("The computer is Scissors. You are Paper. You lose.");<br /> if (number == 1 && guess == 0)<br /> System.out.println("The computer is Rock. You are Scissors. You lose.");<br /> if (number == 1 && guess == 1)<br /> System.out.println("The computer is Rock. You are Rock too. It is a draw.");<br /> if (number == 1 && guess == 2)<br /> System.out.println("The computer is Rock. You are Paper. You won.");<br /> if (number == 2 && guess == 0)<br /> System.out.println("The computer is Paper. You are Scissors. You won.");<br /> if (number == 2 && guess == 1)<br /> System.out.println("The computer is Paper. You are Rock. You lose.");<br /> if (number == 2 && guess == 2)<br /> System.out.println("The computer is Paper. You are Paper too. It is a draw.");<br /> }<br />}<br /> |
1 2 |
Program sample run |
1 2 |