Write a Java program that allows the user to play the game or Rock, Paper Scisso
ID: 3743232 • Letter: W
Question
Write a Java program that allows the user to play the game or Rock, Paper Scissors 5 times.
Remember: Rock crushes scissors, Scissors cuts paper, Paper hides rock.
A sample template has been provided with pseudocode. If you know how to do this problem without using the template, please do so. Otherwise, copy the code into your editor and follow the instructions below:
- Name your program file: xxxx_C5Lab1.java, where xxxx is your kean email id.
- Please hand in hand in java file and screen print of output.
-The assignment will be completed in pairs.
- In a header (comment at top of program), include your name, the name of you and
your partner, the date and a description of the assignment. Each student must hand in their own copy.
The assignment is due before start of next class. We will go over the assignment at the start of the next. Once we go over the assignment, it can only be handed in for 12 credit before the following class.
Sample run:
Enter 1 for Rock, 2 for Paper, 3 for Scissors
1
Player picked Rock, Computer Paper. Computer wins Enter 1 for Rock, 2 for Paper, 3 for Scissors
2
Player picked paper, Computer Scissors. Computer wins Enter 1 for Rock, 2 for Paper, 3 for Scissors
3
Player picked Scissors, Computer Paper. Player wins Enter 1 for Rock, 2 for Paper, 3 for Scissors
1
Tie
Enter 1 for Rock, 2 for Paper, 3 for Scissors
2
Player picked paper, Computer Scissors. Computer wins
Computer wins: 3 Player wins: 1
The Following Code:
import java.util.Scanner;
public class RPS {
//This class allows a player to play RPS 5 times
public static void main(String[] args) {
Scanner reader = new Scanner (System.in);
//variables for player choice and computer choice
int player = 0, computer = 0;
//score variables
int computerScore = 0, playerScore =0;
//Add a loop to play the game of RPS 5 times.
//Pseudocode for 1 game of RPS
//Generate the computer's choice
//1 = rock
//2 = paper
//3 = scissors
computer = (int)(Math.random() * 3) + 1; //computer choice
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();
//player choice
//Complete the code to determine who wins and add to computer or player score
//Rock crushes scissors, paper hides rock, scissors cuts paper
// if player is equal to computer => tie
// if player picks Rock then
// if computer choice is paper, computer wins
// if computer choice is scissors, player wins
// otherwise if player picks paper then
// if computer choice is rock,player wins
// if computer choice is scissors, computer wins
// otherwise if player picks scissors then
// if computer choice is rock, computer wins
// if computer choice is paper, player wins
// otherwise, the player picked an invalid choice. Do not count the game.
//End of loop here.
//print number of computer wins and player wins
}
}
Explanation / Answer
/*
* This is the header. Add your name here
*/
package org.pramit.test;
import java.util.Scanner;
public class RPS {
//This class allows a player to play RPS 5 times
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
//variables for player choice and computer choice
int player = 0, computer = 0;
//score variables
int computerScore = 0, playerScore =0;
//1 for Rock, 2 for Paper, 3 for Scissors
int rock = 1, paper = 2, scissors = 3;
for(int i = 1 ; i <= 5 ; i++) {
computer = (int)(Math.random() * 3) + 1;//computer chooses randomly
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();//player reading input what we will give
//Now the comparison begins
if(computer == player) {
System.out.println("Tie. ");
continue;//going back to loop again
}
if(player == rock) {
if(computer == paper) {
System.out.print("Player picked Rock , Computer Paper. ");
System.out.println("Computer wins");
computerScore++;
}
else if(computer == scissors) {
System.out.print("Player picked Rock , Computer Scissors. ");
System.out.println("Player wins");
playerScore++;
}
}
else if(player == paper) {
if(computer == rock) {
System.out.print("Player picked Paper , Computer Rock. ");
System.out.println("Player wins");
playerScore++;
}
else if(computer == scissors) {
System.out.print("Player picked Paper , Computer Scissors. ");
System.out.println("Computer wins");
computerScore++;
}
}
else if(player == scissors) {
if(computer == paper) {
System.out.print("Player picked Scissors , Computer Paper. ");
System.out.println("Player wins");
playerScore++;
}
else if(computer == rock) {
System.out.print("Player picked Scissors , Computer Rock. ");
System.out.println("Computer wins");
computerScore++;
}
}
else {
System.out.println("Player picked an invalid choice");
continue;
}
} //for loop ends
System.out.println("Computer wins: " + computerScore + " Player wins: " + playerScore);
reader.close();
} //main ends
} //class ends
If you need the screenshot of ouput, let me now in the comment section. Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.