Create a program that plays the game Paper, Rock, Scissors and a diagram for all
ID: 3674212 • Letter: C
Question
Create a program that plays the game Paper, Rock, Scissors and a diagram for all switch and if statements. 1. The first part of the program should allow the user to play against the computer. And displays the winner of the game. We are going to assume that Paper is ‘1’, Rock is ‘2’ and Scissors is ‘3’. Compare the users answer to the computer’s answer. The User The user should be able to enter a number in. Please make this feature user friendly for the user. The Computer We will be using the Random Number Generator to make the computer decide between 1-Paper, 2-Rock, and 3-Scissors. 2. Create a user menu using switch statements OPTIONS I. Play Paper, Rock, Scissors versus the Computer II. Print out the Statistics versus the Computer III. Quit the Program All other numbers entered should be reads as BAD DATA!!! For option number two, you will have to go back into you code for task one and keep track of the user’s wins and the Computers Wins and calculate the Winning Percentage and print out all draws for the current session. The program should continuously run (The code will be provided for the continuous run).
Explanation / Answer
java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class Game {
public static void main(String[] args) throws java.lang.Exception {
Scanner Tlya = new Scanner(System. in );
Random random = new Random();
int inp;
int b=1;
System.out.println("Choose 1 or 2 or 3");
System.out.println("Rock 1 or Paper 2 or Scissor 3");
while(b!=0) {
int rk = 1, ppr = 2, scisr = 3;
inp = Tlya.nextInt();
int randNum = random.nextInt(3-1+1)+1;
if (randNum == rk) {
if (inp == rk) {
System.out.println("Rock vs Rock - GAME TIE");
} else if (inp == ppr) {
System.out.println("Paper vs Rock - GAME WON");
} else if (inp == scisr) {
System.out.println("Scissor vs Rock - GAME LOST");
}
} else if (randNum == ppr) {
if (inp == rk) {
System.out.println("Rock vs Paper - GAME LOST");
} else if (inp == ppr) {
System.out.println("Paper vs Paper - GAME TIE");
} else if (inp == scisr) {
System.out.println("Scissor vs Paper - GAME WON");
}
} else if (randNum == scisr) {
if (inp == rk) {
System.out.println("Rock vs Scissor - GAME WON");
} else if (inp == ppr) {
System.out.println("Paper vs Scissor - GAME LOST");
} else if (inp == scisr) {
System.out.println("Scissor vs Scissor - GAME TIE");
}
}
int Y=1, N=2;
System.out.println("Do you wish to continue? Y(1)/N(2)");
inp = Tlya.nextInt();
if(inp==Y){
b=1;
System.out.println("Rock,Paper,Scissor");
}
else if(inp==N)
{System.exit(0);
System.out.println("BYE. HAVE A NICE DAY!");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.