Question: In this problem, you'll play a simple rock, paper, scissors game. First, you'll ask the user to pick rock, paper, or scissors. Then, you'll have
In this problem, you'll play a simple rock, paper, scissors
game. First, you'll ask the user to pick rock, paper, or
scissors.
Then, you'll have the computer randomly choose one of the
options. You should use theRandomizerclass that you created in the last exercise to help the computer make a choice.
After that, print out the winner! You should keep playing the game until the user hits enter.
Note: You'll need to implement a method called
String getWinner(String user, String computer)that determines whether the user or computer won the game, and return the correct winner!
Here is a sample run of the program.
Enter your choice (rock, paper, or scissors): rock User: rock Computer: rock Tie Enter your choice (rock, paper, or scissors): paper User: paper Computer: rock User wins! Enter your choice (rock, paper, or scissors): Thanks for playing!
My code so far is this:
import java.util.Scanner;
public class RockPaperScissors
{
private static final String USER_PLAYER = "User wins!";
private static final String COMPUTER_PLAYER = "Computer wins!";
private static final String TIE = "Tie";
public static String getWinner(String user, String computer)
{
int min = 1;
int max = 3;
Scanner input = new Scanner(System.in);
System.out.println("Enter your choice (rock, paper, or scissors):");
String firstChoice = input.nextLine();
System.out.println("User: " + firstChoice);
System.out.println("Computer: ");
String compChoice = "";
if((Randomizer.nextInt(min, max)) == 1) {
compChoice ="rock";
System.out.print(compChoice);
}
else if ((Randomizer.nextInt(min, max)) == 2) {
compChoice = "paper";
System.out.print(compChoice);
}
else if ((Randomizer.nextInt(min, max)) == 3) {
compChoice ="scissors";
System.out.print(compChoice);
}
}
public static void main(String[] args)
{
getWinner();
}
}
public class Randomizer
{
public static int nextInt()
{
//Implement this method to return a random number from 1-10
int randInteger = (int)(Math.random()* (10) + 1);
return randInteger;
}
public static int nextInt(int min, int max)
{
//Implement this method to return a random integer between min and max
int nextInteger = (int)(Math.random()*(max - min) + min);
return nextInteger;
}
}
I do not know how to properly implement the static method, can someone please help?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
