I need a Rock, paper, scissors program through java that has these requirements:
ID: 640553 • Letter: I
Question
I need a Rock, paper, scissors program through java that has these requirements:
Declare String constants for "rock", "paper" and "scissors" and use these constants in the code.
The user's choice must be read as a String.
The program should not be case sensitive. That is an entry of uppercase P (for paper) is equivalent to an entry of paper or lowercase p, etc.
The program must validate the user's entry, printing an error message and exiting if invalid.
Example Output 1: Let's play Rock, Paper, Scissors!
The computer has made a choice, but it is a secret for now!
Enter your choice of R for Rock, Pfor Paper or S for Scissors: R
Okay, I got that. You chose Rock!
Now I can reveal that the Computer chose Scissors and determine a winner...
Game results: You won! Rock beats Scissors...
Example Output 2: Let's play Rock, Paper, Scissors!
The computer has made a choice, but it is a secret for now! Enter your choice of R for Rock, P for Paper or S for Scissors: P
Okay, I got that. You chose Paper!
Now I can reveal that the Computer chose Paper and determine a winner...
Game results: It's a tie!
Example Output 3: Let's play Rock, Paper, Scissors!
The computer has made a choice, but it is a secret for now!
Enter your choice of R for Rock, P for Paper or S for Scissors: p
Okay, I got that. You chose Paper !
Now I can reveal that the Computer chose Scissors and determine a winner...
Game results: Sorry, the computer won! Scissors beats Paper...
Example Output 4: Let's play Rock, Paper, Scissors!
The computer has made a choice, but it is a secret for now!
Enter your choice of R for Rock, P for Paper or S for Scissors: O
Okay, I got that. Sorry but your choice is invalid. Good-bye!
Must use this random number generator as well:
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class Rps {
public static String computerChoice() {
// Variable to hold the computer's choice.
String choice;
// Create a Random object.
Random rand = new Random();
// Generate a random number in the range of
// 1 through 3.
int num = rand.nextInt(3) + 1;
// Determine the computer's choice where
// 1=rock, 2=paper, or 3=scissors.
switch (num) {
case 1:
choice = "rock";
break;
case 2:
choice = "paper";
break;
case 3:
choice = "scissors";
break;
default:
choice = "invalid";
}
// Return the computer's choice.
return choice;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String choice = computerChoice();
System.out.println("Let's play Rock, Paper, Scissors!");
System.out.println("The computer has made a choice, but it is a secret for now!");
System.out.print("Enter your choice of R for Rock, P for Paper or S for Scissors: ");
String option = in.nextLine();
System.out.print("Okay, I got that. ");
char ch = option.toLowerCase().charAt(0);
switch (ch) {
case 'r':
System.out.println("You chose Rock!");
option = "rock";
break;
case 'p':
System.out.println("You chose Paper!");
option = "paper";
break;
case 's':
System.out.println("You chose Scissors!");
option = "scissors";
break;
default:
System.out.println("Sorry but your choice is invalid. Good-bye!");
}
System.out.println("Now I can reveal that the Computer chose " + choice
+ " and determine a winner...");
if ((option == "rock" && choice == "scissors")
|| (option == "paper" && choice == "rock")
|| (option == "scissors" && choice == "paper")) {
System.out.println("Game results: You won! " + option + " beats " + choice + "...");
} else if ((option == "paper" && choice == "scissors")
|| (option == "scissors" && choice == "rock")
|| (option == "rock" && choice == "paper")) {
System.out.println("Game results: Sorry, the computer won! " + choice + " beats " + option + "...");
} else {
System.out.println("Game results: It's a tie!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.