如何通过布尔值传递字符串?是为了真,否为了假?

huangapple go评论79阅读模式
英文:

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:

  1. Used .equalsIgnoreCase("yes") to handle the user input "yes" or "no" regardless of the case.
  2. Removed unnecessary curly braces after the else statements, 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

huangapple
  • 本文由 发表于 2020年10月15日 08:21:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64363172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定