This question already has an answer here:
- How do I compare strings in Java? 23 answers
I'm probably missing something really basic but can't understand why despite my input my if-statement just goes directly to my else-statement.
Here is my code:
for(int i = 0; i<10; i++){
String input=UserInput.getUserInput();
if(input == "field"){
player.moveTo(field);
}
if(input == "forest"){
player.moveTo(forest);
}
if(input == "ocean"){
player.moveTo(ocean);
}
if(input == "cave"){
player.moveTo(cave);
}
if(input == "room"){
player.moveTo(room);
}
else{
System.out.println("Incorrect location!");
}
player.getLocation().describeYourself();
}
And here is my UserInput class:
import java.util.Scanner;
public class UserInput {
public static String getUserInput(){
Scanner in = new Scanner (System.in);
String input = in.nextLine();
return input;
}
}
Aucun commentaire:
Enregistrer un commentaire