I'm trying to write my first program that uses booleans and if/else statements. It takes the user through various questions and ultimately tells them whether they should eat at home or at a restaurant. This is done through a Scanner object where the user enters "true" or "false". It seems to work well but my question is, how do I allow the user to enter "yes" for "true" and "no" for "false", instead of having them type those words? My assignment specifies they should use yes/no.
import java.util.Scanner;
public class HomeOrRestaurant {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("yes/no");
System.out.println("Did you get paid this week?");
boolean paid = scan.nextBoolean();
if (!paid == true){
System.out.println("Eat at home.");
return;
}
else
System.out.println("Did you buy groceries this week?");
boolean boughtGroceries = scan.nextBoolean();
if (boughtGroceries == true){
System.out.println("Eat at home.");
return;
}
else
System.out.println("Do you have leftovers at home?");
boolean leftovers = scan.nextBoolean();
if (!leftovers == true){
System.out.println("Eat at home.");
return;
}
else
System.out.println("Are there enough leftovers for a meal?");
boolean enoughLeftovers = scan.nextBoolean();
if (!enoughLeftovers == true){
System.out.println("Eat at a restaurant.");
}
else
System.out.println("Eat at home.");
}
}
Upon trying to set one of these to "yes" or "no"...
System.out.println("Did you get paid this week?");
boolean paid = scan.nextBoolean();
if (!paid == yes){
System.out.println("Eat at home.");
return;
}
I get an error telling me that the symbol variable "yes" could not be found. In response I tried to create a local variable "yes" but this did not work. How do I tell my program that "yes" means "true"? Thanks!
Aucun commentaire:
Enregistrer un commentaire