Question
Is the random interger new Random? Should the statement look like- switch (new Random) case r: break; case p: break; case s; break; default: import
Is the random interger new Random?
Should the statement look like-
switch (new Random)
case r:
break;
case p:
break;
case s;
break;
default:
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args){
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine computer's play
Random generator = new Random();
Scanner scan = new Scanner (System.in);
//Get player's play -- note that this is stored as a string
System.out.print ("Enter your play: R, P, or S: ");
personPlay = scan.nextLine();
//Make player's play uppercase for ease of comparison
personPlay = personPlay.toUpperCase();
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3);
//Translate computer's randomly generated play to string
//"R", "P", or "S". If play is not a value you expected (0,1,2),
//set it to "I" and print a warning message.
// WRITE A SWITCH STATEMENT THAT USES THE RANDOM INTEGER
// GENERATED IN computerInt TO ASIGN computerPlay THE LETTERS "R", "P",
// "S"
// MAKE THE DEFAULT OF THE SWITCH STATEMENT ASIGN computerPlay THE LETTER "I"
// AND PRINT AN UNEXPEXTED RANDOM NUMBER MESSAGE TO THE CONSOLE
//Print computer's play
System.out.println("Computer play is " + computerPlay);
// USING AN ELSE IF CLAUSE, PRINT AN "Illegal person play" MESSAGE TO CONSOLE IF
// personPlay IS NOT A "P", "R", OR "S"
// ELSE IF computerPlay IS AN "I" PRINT AN "Illegal computer play" MESSAGE TO CONSOLE
// OTHERWISE, DECIDE WHO WON (HINT USE NESTED IFS), REMEMBER:
// ROCK BEATS SCISSOR
// SCISSOR BEATS PAPER
// PAPER BEATS ROCK
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started