I have a simple code in java that always returns "Game over."
.
import java.util.Scanner;
public class nextQuestion {
public void newQuestion() {
Scanner choice2 = new Scanner(System.in);
System.out.println("What is my name?");
System.out.println("A. Mark");
System.out.println("B. Robert");
System.out.println("C. Mio");
System.out.println("D. Samantha");
String answer2 = choice2.nextLine();
if(answer2 == "a" || answer2 == "A") {
System.out.println("Game over.");
System.exit(0);
} else if(answer2 == "b" || answer2 == "B") {
System.out.println("Nice guess!");
System.exit(0);
} else if(answer2 == "c" || answer2 == "C") {
System.out.println("Game over.");
System.exit(0);
} else if(answer2 == "d" || answer2 == "D") {
System.out.println("Game over.");
System.exit(0);
} else {
System.out.println("Game over.");
System.exit(0);
}
}
}
However if I use switch statement, it does work. What is the problem with this?