This question already has an answer here:
- How do I compare strings in Java? 23 answers
I am trying to get my if/else if statement to change the "garaje" variable into the correct string based on the user's string response. Yet, every time I compile no errors are returned back to me. When I try to execute the code the "garaje" variable always returns back the else statement: "do not understand what a garage is... do you?". I am wondering if perhaps I misused the nextLine() method when linking it to the "garaje" variable. I tried using next() and the same result was acquired. Please help...thanks.
import java.util.Scanner;
public class House {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many bedrooms in your home? ");
int cuartos = input.nextInt();
System.out.println("How many bathrooms? ");
int banos = input.nextInt();
System.out.println("How many living rooms? ");
int salas = input.nextInt();
System.out.println("How many kitchens? ");
int cocina = input.nextInt();
System.out.println("Does your house have a garage (Yes or No)? ");
String garaje = input.nextLine();
if ((garaje == "Yes") || (garaje == "yes") || (garaje == "YES")) {
garaje = "do have a garage.";
} else if ((garaje == "No") || (garaje == "no") || (garaje == "NO")) {
garaje = "do not have a garage.";
} else {
garaje = " do not understand what a garage is...do you?";
}
System.out.println("You have " + cuartos + " bedrooms, " + banos + " bathrooms, " +salas + " living rooms, " + cocina + "
kitchens, " + " and you " + garaje);
}
}
Aucun commentaire:
Enregistrer un commentaire