// ************************************************** **************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
// ************************************************** **************
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay = ""; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine
//computer's play
Scanner scan = new Scanner(System.in);
Random generator = new Random();
//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3);
//Translate computer's randomly generated play to string using if
//statements or a switch statement
switch (computerInt)
{
case 0: computerPlay = "R"; break;
case 1: computerPlay = "P"; break;
case 2: computerPlay = "S"; break;
}
//Get player's play from input-- note that this is stored as a string
System.out.println ("Choose: R(Rock), P(Paper), S(Scissor): ");
personPlay = scan.nextLine();
//Make player's play uppercase for ease of comparison
personPlay = personPlay.toUpperCase();
//Print computer's play
System.out.println ("Player1: " + personPlay);
//See who won. Use nested ifs
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
{
System.out.println ("Paper covers rock. You Lose!");
}
}
else if (personPlay.equals("P"))
{
if (computerPlay.equals("R"))
{
System.out.println ("Paper cover rock. You Win!");
}
else
{
System.out.println ("Scissors cut paper. You Lose!");
}
}
// The error is with these statements below. They are executed even if the the conditions are not met.
else if (personPlay.equals("S"));
{
if (computerPlay.equals("R"))
{
System.out.println ("Rock crushes scissor. You Lose!");
}
else
{
System.out.println ("Scissors cut paper. You Win!");
}
}
} // end of method
}// end of class