Requirements: The player can enter either “rock”, “paper”, or “scissors”. If the
ID: 668684 • Letter: R
Question
Requirements:
The player can enter either “rock”, “paper”, or “scissors”.
If the player enters anything other than that the computer automatically gets a point
The computer randomly selects one of the gestures
Use the Random type to make this easier
Also make sure you import java.util.Random
You can use randomly selected integers to represent the gestures
For each combination either the computer scores a point, the player scores a point, or they score neither on draws.
Rock vs Paper = Paper wins
Paper vs Scissors = Scissors wins
Scissors vs Rock = Rock wins
After 3 rounds the winner or a tie is declared
The player is then asked whether or not they want to play again, and if they do the whole process starts over.
---
So, far, my program runs smoothly except for two issues. It does not play for just three rounds and I need it to play for just 3 rounds and determine a winner based off of whoever won at least once during the three rounds despite ties (if it comes down to that scenario randomly). It plays until there is a 2 to 1 winning ratio for either the player or the computer. I do not want it to run continuously like this. The other issue is that once a final winner is declared, the line “Enter rock, paper, or scissors” appears again, and it is not needed there. How do I go about fixing these two issues? Thank you.
--
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Prompt user to enter rock, paper, or scissors
//Compare random value
while(true){
int wins = 0;
int losses = 0;
int rnd = 0;
int USER = 0;
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! "
+ "Enter "Rock", "Paper" or "Scissors" ");
// Plays 3 rounds before terminating
while(rnd<3) {
Random GAME = new Random();
int PC = 1+GAME.nextInt(3);
Scanner keyboard = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
ROCK = 1;
PAPER = 2;
SCISSOR= 3;
String USER_Input = keyboard.next();
if(USER_Input.equals("Rock")) {
USER = 1;
}
if(USER_Input.equals("Paper")) {
USER = 2;
}
if(USER_Input.equals("Scissors")) {
USER = 3;
}
//If the user enters a value greater then 3 or less than 1 it will terminate the program
//and display an error message
while (USER > 3 || USER < 1) {
System.err.println("Incorrect value entered.");
System.exit(0);
break;
}
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Player wins
if(USER == SCISSOR)
if(PC == PAPER){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Computer wins
else if(PC == ROCK){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Player wins
if(USER == ROCK)
if(PC == SCISSOR ){
System.out.println("Rock v Scissor! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Computer wins
else if (PC == PAPER){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Player Wins
if(USER == PAPER)
if(PC == ROCK){
System.out.println("Paper v Rock! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Computer Wins
else if (PC == SCISSOR){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
}
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? "Yes" or "No"");
Scanner YN = new Scanner(System.in);
String YN_String = YN.next();
if(YN_String.equals("Yes") || YN_String.equals("yes")){
}if(YN_String.equals("No") || YN_String.equals("no")) {
System.out.println ("Goodbye!");
break;
}
}
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Prompt user to enter rock, paper, or scissors
//Compare random value
while(true){
int wins = 0;
int losses = 0;
int rnd = 0;
int USER = 0;
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! "
+ "Enter "Rock", "Paper" or "Scissors" ");
// Plays 3 rounds before terminating
while(rnd<3) {
Random GAME = new Random();
int PC = 1+GAME.nextInt(3);
Scanner keyboard = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
ROCK = 1;
PAPER = 2;
SCISSOR= 3;
String USER_Input = keyboard.next();
if(USER_Input.equals("Rock")) {
USER = 1;
}
if(USER_Input.equals("Paper")) {
USER = 2;
}
if(USER_Input.equals("Scissors")) {
USER = 3;
}
//If the user enters a value greater then 3 or less than 1 it will terminate the program
//and display an error message
while (USER > 3 || USER < 1) {
System.err.println("Incorrect value entered.");
System.exit(0);
break;
}
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Player wins
if(USER == SCISSOR)
if(PC == PAPER){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Computer wins
else if(PC == ROCK){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Player wins
if(USER == ROCK)
if(PC == SCISSOR ){
System.out.println("Rock v Scissor! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Computer wins
else if (PC == PAPER){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Player Wins
if(USER == PAPER)
if(PC == ROCK){
System.out.println("Paper v Rock! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
//Computer Wins
else if (PC == SCISSOR){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter "Rock", "Paper" or "Scissors" ");
}
}
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? "Yes" or "No"");
Scanner YN = new Scanner(System.in);
String YN_String = YN.next();
if(YN_String.equals("Yes") || YN_String.equals("yes")){
}if(YN_String.equals("No") || YN_String.equals("no")) {
System.out.println ("Goodbye!");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.