英文:
How to pass Strings through Booleans? yes for true, no for false?
问题
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.next().equalsIgnoreCase("yes");          
        if (!paid) {
            System.out.println("Eat at home.");
            return;
        }
        else {
            System.out.println("Did you buy groceries this week?");
            boolean boughtGroceries = scan.next().equalsIgnoreCase("yes"); 
            if (boughtGroceries) {
                System.out.println("Eat at home.");
                return;
            }
            else {
                System.out.println("Do you have leftovers at home?");
                boolean leftovers = scan.next().equalsIgnoreCase("yes"); 
                if (!leftovers) {
                    System.out.println("Eat at home.");
                    return;
                }
                else {
                    System.out.println("Are there enough leftovers for a meal?");
                    boolean enoughLeftovers = scan.next().equalsIgnoreCase("yes"); 
                    if (!enoughLeftovers) {
                        System.out.println("Eat at a restaurant.");
                    }
                    else {
                        System.out.println("Eat at home.");
                    }
                }
            }
        }
    }  
}
Please note that I've made the following changes to your code:
- Used 
.equalsIgnoreCase("yes")to handle the user input "yes" or "no" regardless of the case. - Removed unnecessary curly braces after the 
elsestatements, as they were causing the code to behave unexpectedly. 
英文:
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!
答案1
得分: 2
如果您想输入一个字符串,不要使用nextBoolean,可以使用nextLine或者next。
当您获得字符串后,可以测试它是否等于yes。
if (paid.equalsIgnoreCase("YES"))
编辑
由于您经常使用这个模式,我建议您将这段代码封装成自己的方法。
英文:
If you want to enter a String, then do not use nextBoolean, maybe use nextLine or next
When you have your String you can test if it is equal to yes
if (paid.equalsIgnoreCase ("YES"))
edit
As you are using this pattern a lot, I suggest that you wrap this code up into its own method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论