This question already has an answer here:
- How do I compare strings in Java? 23 answers
This code is supposed to evaluate to "Its me!..." statement if the parameters inputed are "White" AND "Fat" AND "Used" but for some reason I keep getting "Oh, shucks" regardless of what I input. Im not sure how to get it to work properly.
Does it have to do something about the way I initialized the variables or the way I implemented the scanner?
public static void main(String[] args) {
String color = "";
String slenderOrFat = "";
String usedOrNew = "";
System.out.println("What Color? "); // Prompting the user for a color input
Scanner whatColor = new Scanner(System.in); // Reading from System.in
color = whatColor.nextLine(); // Setting variable to user input value
System.out.println(color);
System.out.println("Slender or Fat? "); // Prompting the user for a "Slender" or "Fat" input
Scanner slender_fat = new Scanner(System.in); // Reading from System.in
slenderOrFat = slender_fat.nextLine(); // Setting variable to user input value
System.out.println(slenderOrFat);
System.out.println("Used or New? "); // Prompting the user for a "Used" or "New" input
Scanner used_new = new Scanner(System.in); // Reading from System.in
usedOrNew = used_new.nextLine(); // Setting variable to user input value
System.out.println(usedOrNew);
if (color == "White" && slenderOrFat == "Fat" && usedOrNew == "Used" ){ //The conditions of the selection control structure
System.out.println("It's me! It's me! It's me!"); //This is printed if Stubee is found
}
else{
System.out.println("Oh, shucks"); //This is printed otherwise
}
}
}
Aucun commentaire:
Enregistrer un commentaire