vendredi 28 février 2020

Java: Why wont this if-then statement read my string correctly?

I am a very new java programmer and I can't seem to figure out why my if-then statement isn't working. I want the program to display different text depending on whether the user is a boy or girl. The problem is that whenever I test for either of these inputs, they get skipped and I get the else statment's output that prints "INVALID INPUT". Even if the user's input is correct, why wont it test for either boy or girl?

Thanks a bunch!

         Scanner userInput = new Scanner(System.in);
         System.out.println("Are you a boy or a girl?");

         gender = userInput.nextLine();
         gender = gender.toLowerCase();
         System.out.println(gender);
         if (gender == "boy") {
             System.out.println("You decide that you should try to make some short of primitive shelter for the night. Thanks to you being a boy scout, you have some knowledge of how to build");
             System.out.println("a lean-to.");
         } else if (gender == "girl") {
             System.out.println("You decide that you should try to make some short of primitive shelter for the night. Thanks to that survival book you read last summer, you have some knowledge");
             System.out.println("of how to build a lean-to.");

         } else {
             System.out.println("INVALID INPUT");
         }

         System.out.println("next");
     }```


Aucun commentaire:

Enregistrer un commentaire