I'm relatively new to Java, and I'm trying to make a simple donut shop project. As you can see in my code, I take input from the user, asking which donut they'd like. I then have an if statement checking if they said chocolate. I've done debugging and can clearly see that even when the input and "chocolate" are identical to each other, the if statement does not return as true. I'm assuming that the input might have a different dataType which isn't a string, but I'm not sure. Here's my code:
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Best Donuts! Would you like chocolate, vanilla, or glazed: ");
String chosenDonut = input.nextLine();
System.out.println(chosenDonut);
if (chosenDonut == "chocolate") {
System.out.println("Would you like sprinkles with that?");
String sprinkleQ = input.nextLine();
if (sprinkleQ == "yes") {
System.out.println("Your total is: " + chocolate + sprinkles);
}
}
Aucun commentaire:
Enregistrer un commentaire