Hi guys, I'm working on a program which is essentially Rock Paper Scissors Vs. the computer.
Anyway I'm having a bit of difficulty with it and could use some assistance.
I need to use a class method, so I decided to separate the computer's choice in the class method, I'd then like to take that random choice and store it in computerPlay (line 27)
I'm not sure how to return the info in the class method.
Here's the code so far, any help is appreciated!
//RockPaperScissors.java
//
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
// Prompts user to choose: rock,paper, or scissors
// Computer pseudo-randomly picks rock, paper, or scissors
// Prints out who won, keeps going until user wins
// Prints out how many times it took for user to beat computer
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Random generator = new Random();
String personPlay; //User's choice - R, P, or S
String computerPlay; // Computer's choice - R, P, or S
int computerInt; // Random generated number to choose computer's choice
System.out.println("Enter R for Rock, P for Paper, S for Scissors: ");
personPlay = scan.nextLine();
personPlay = personPlay.toUpperCase();
computerPlay = pickHandShape();
}
System.out.println("Computer plays: "+ computerPlay);
if (personPlay.equals(computerPlay))
{
System.out.println("It's a tie!");
}
else if (personPlay.equals("R"))
{
if (computerPlay.equals("S"))
System.out.println("Rock crushes scissors. You win!!");
else if (computerPlay.equals("P"))
System.out.println ("Paper eats rock. You lose!!");
}
else if (personPlay.equals("P"))
{
if (computerPlay.equals("S"))
System.out.println ("Scissor cuts paper. You lose!!");
else if (computerPlay.equals("R"))
System.out.println ("Paper eats rock. You win!!");
}
else if (personPlay.equals("S"))
{
if (computerPlay.equals("P"))
System.out.println ("Scissor cuts paper. You win!!");
else if (computerPlay.equals("R"))
System.out.println ("Rock breaks scissors. You lose!!");
}
//Chooses Computer Choice
public static String pickHandShape()
{
computerInt = generator.nextInt(3);
switch (computerInt)
{
case 0:
{
computerPlay = "R";
break;
}
case 1:
{
computerPlay = "P";
break;
}
case 2:
{
computerPlay = "S";
break;
}
default:
{
computerPlay = "will not happen";
}
}
}
}