Use brackets :
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 (personPlay.equals("P")) {
if (computerPlay.equals("R"))
System.out.println("Paper wraps around rock. You win!");
} else if (personPlay.equals("S")) {
if (computerPlay.equals("P"))
System.out.println("Scissors cut paper. You win!");
} else {
System.out.println("you lose");
}
As mentioned in comments, this code would still not work, since the logic is flawed. You should either have an else clause in each internal if
, in which you should print "you lose" :
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("you lose");
} else if (personPlay.equals("P")) {
if (computerPlay.equals("R"))
System.out.println("Paper wraps around rock. You win!");
else
System.out.println("you lose");
} else if (personPlay.equals("S")) {
if (computerPlay.equals("P"))
System.out.println("Scissors cut paper. You win!");
else
System.out.println("you lose");
} else {
System.out.println("invalid input");
}
Or you could eliminate the inner if
s and use &&
instead :
if (personPlay.equals(computerPlay)) {
System.out.println("It's a tie!");
} else if (personPlay.equals("R") && computerPlay.equals("S")) {
System.out.println("Rock crushes scissors. You win!!");
} else if (personPlay.equals("P") && computerPlay.equals("R")) {
System.out.println("Paper wraps around rock. You win!");
} else if (personPlay.equals("S") && computerPlay.equals("P")) {
System.out.println("Scissors cut paper. You win!");
} else {
System.out.println("you lose");
}