I'm making a game of rock paper scissors for my java class, but when I ask it to take a string, the program just terminates after running. I had it previously set to take a char instead and it worked every time. Where have I gone wrong?
P.S. If you wouldn't mind showing me how to insert a play again option that'd be great.
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Choose your hand (rock, paper, scissors, lizard, Spock):");
String hand = input.nextLine().toUpperCase();
int opponent = (int)(Math.random() * ((4) + 1));
if (hand == "ROCK") {
if (opponent == 0 ) {
System.out.println("Rock does nothing to rock. Tie.");
}
else if (opponent == 1) {
System.out.println("Paper covers rock. You Lose...");
}
else if (opponent == 2) {
System.out.println("Rock smashes scissors. You Win! ");
}
else if (opponent == 3) {
System.out.println("Rock smashes lizard. You Win!");
}
else {
System.out.println("Spock vaporizes rock. You Lose...");
}
}
if (hand == "PAPER") {
if (opponent == 0 ) {
System.out.println("Paper covers rock. You Win!");
}
else if (opponent == 1) {
System.out.println("paper does nothing to paper. Tie.");
}
else if (opponent == 2) {
System.out.println("Scissors cuts paper. You Lose...");
}
else if (opponent == 3) {
System.out.println("Lizard eats paper. You Lose...");
}
else {
System.out.println("Paper disproves Spock. You Win!");
}
}
if (hand == "SCISSORS") {
if (opponent == 0 ) {
System.out.println("Rock smashes Scissors. You Lose...");
}
else if (opponent == 1) {
System.out.println("Scissors cuts paper. You Win!");
}
else if (opponent == 2) {
System.out.println("Scissors do nothing to scissors. Tie.");
}
else if (opponent == 3) {
System.out.println("Scissors decapitates lizard. You Win!");
}
else {
System.out.println("Spock smashes scissors. You Lose...");
}
}
if (hand == "LIZARD") {
if (opponent == 0 ) {
System.out.println("Rock smashes lizard. You Lose...");
}
else if (opponent == 1) {
System.out.println("Lizard eats paper. You Win!");
}
else if (opponent == 2) {
System.out.println("Scissors decapitates lizard. You Lose...");
}
else if (opponent == 3) {
System.out.println("Lizard does nothing to lizard. Tie.");
}
else {
System.out.println("Lizard poisons Spock. You Win!");
}
}
if (hand == "SPOCK" ) {
if (opponent == 0 ) {
System.out.println("Spock vaporizes rock. You Win!");
}
else if (opponent == 1) {
System.out.println("Paper disproves Spock. You Lose...");
}
else if (opponent == 2) {
System.out.println(" Spock smashes scissors. You Win!");
}
else if (opponent == 3) {
System.out.println("Lizard poisons Spock. You Lose...");
}
else {
System.out.println("Spock does nothing to Spock. Tie");
}
if (hand != "ROCK" && hand != "PAPER" && hand != "SCISSORS" && hand != "LIZARD" && hand != "SPOCK") {
System.out.println("Invalid hand.");
}
}
}
}