简体   繁体   English

是/否,布尔值还是 if/else?

[英]Yes/No with boolean or if/else?

I'm having issues creating a yes/no input in my program.我在我的程序中创建是/否输入时遇到问题。 I want the user to be able to input ay/n after the last print statement, but i'm not quite sure how I would implement it.我希望用户能够在最后一个打印语句之后输入 ay/n,但我不太确定我将如何实现它。 I know it would most likely include a boolean or an if/else statement, but i'm not quite sure.我知道它很可能包含一个布尔值或一个 if/else 语句,但我不太确定。 Any help?有什么帮助吗?

import java.util.*;
import java.io.*; 
import java.util.Scanner;

public class Person 
{
public static void main(String[]args) 
{

    int age;
    String name;

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter in your age.");
    age = scan.nextInt();

        if (age >= 10 && age < 18) 
        {
            System.out.println("So you're a kid, huh?");
        } 

        else if (age < 10)
        {
            System.out.println("Nice try.");
            System.exit(0);
        }
        else if (age >= 18 && age <= 100)
        {
            System.out.println("So you're an adult, huh?");
        }
        else if (age > 100)
        {
            System.out.println("Nice try.");
            System.exit(0);
        }

        Scanner in = new Scanner(System.in);
        System.out.println("Enter in your name");
        name = in.nextLine();

        System.out.println("So you're " + age + " years old and your name is " + name + "?");

        System.out.println("y/n");
        }
}
String answer;
boolean yn;

System.out.println("y/n");
while (true) {
  answer = in.nextLine().trim().toLowerCase();
  if (answer.equals("y")) {
    yn = true;
    break;
  } else if (answer.equals("n")) {
    yn = false;
    break;
  } else {
     System.out.println("Sorry, I didn't catch that. Please answer y/n");
  }
}

Is this what you need?这是你需要的吗?

String answer = in.nextLine()
Boolean yn = (answer.equalsIgnoreCase("Y") ? true : false);

Or, you can use a shorter form:或者,您可以使用更短的形式:

Boolean yn = (answer.equalsIgnoreCase("Y"));

If you meant something else please post a clarification.如果您的意思是其他内容,请发布说明。

just add boolean answer = in.nextLine().toLowerCase().startsWith("y");只需添加boolean answer = in.nextLine().toLowerCase().startsWith("y");

So basically it sets answer to true if you type anything that starts with y .因此,如果您键入以y开头的任何内容,基本上它会将answer设置为 true 。 false otherwise.否则为假。

You would read in a string and compare it to the literal "y".您将读取一个字符串并将其与文字“y”进行比较。 The comparison yields a boolean.比较产生一个布尔值。

You can expand this to first checking if it's "y" or "n" and complain if it's neither or narrow it to accept other equivalents like "yes" and "no".您可以将其扩展为首先检查它是“y”还是“n”,如果两者都不是则抱怨或缩小它以接受其他等价物,如“yes”和“no”。

In any case, your boolean is achieved by comparing to a literal.无论如何,您的布尔值是通过与文字进行比较来实现的。

试试这个,

boolean answer = "y".equalsIgnoreCase(in.nextLine());

You can use do while loop for this like:您可以使用do while loop ,例如:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        int age;
        String name;
        String input = "y";
        do {
            Scanner scan = new Scanner(System.in);

            System.out.println("Enter in your age.");
            age = scan.nextInt();

            if (age >= 10 && age < 18) {
                System.out.println("So you're a kid, huh?");
            }

            else if (age < 10) {
                System.out.println("Nice try.");
                System.exit(0);
            } else if (age >= 18 && age <= 100) {
                System.out.println("So you're an adult, huh?");
            } else if (age > 100) {
                System.out.println("Nice try.");
                System.exit(0);
            }

            Scanner in = new Scanner(System.in);
            System.out.println("Enter in your name");
            name = in.nextLine();

            System.out.println("So you're " + age
                    + " years old and your name is " + name + "?");

            Scanner sc = new Scanner(System.in);
            System.out.println("y/n");
            input = sc.nextLine();
        } while (input.equalsIgnoreCase("y"));
    }
}

I got this figured out.我明白了这一点。

import java.util.Scanner;

public class MuseumHours2 {
    public static void main(String[] args) {
        String day;
        boolean yn = true;
        boolean userContinue;
        String answer;

        Scanner stdIn = new Scanner(System.in);


        do {
            System.out.println("Hello, thank you for visiting the Museum 
    website. For what day would you like to check our hours of operation?");
            day = stdIn.nextLine();

            System.out.println("Is today a holiday? Enter y or n.");
            answer = stdIn.nextLine();
                if (answer.equals("y")) {
                    yn = true;
                } else if (answer.equals("n")) {
                    yn = false;
                } else { 
                    System.out.println("Sorry, I didn't understand. Please reply y or n.");
                }

            switch (day) {
            case "Monday":
                System.out.println("Closed.");
                break;

            case "Tuesday":
                if (yn) {
                    System.out.println("We are open from 1:00 to 4:00.");
                } else {
                    System.out.println("We are open from 12:00 to 5:00.");
                }
                break;
            case "Wednesday":
                if (yn) {
                    System.out.println("We are open from 2:00 to 5:00.");
                } else { 
                    System.out.println("We are open from 1:00 to 6:00.");
                    break;
                }
            case "Thursday":
                System.out.println("Closed.");
                break;
                }
            System.out.println("Do you want to re-run the program? Type y or n.");
        } while (userContinue = "y".equalsIgnoreCase(stdIn.nextLine()));
    }
}

Here is an example这是一个例子

 public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        boolean yn;
        String ageq;

        System.out.println("Please enter your age: ");
        ageq = sc.next();

        System.out.println("You entered " + ageq + " is this correct?");
        System.out.println("y/n");
        OUTER:
        while (true) {
            ageq = sc.next().trim().toLowerCase();
            switch (ageq) {
                case "y":
                    yn = true;
                    break OUTER;
                case "n":
                    yn = false;
                    break OUTER;
                default:
                    System.out.println("Sorry, I didn't get that. Please enter y/n");
                    break;
            }
        }

  }

使用org.apache.commons.lang3.BooleanUtils.toBooleanObject() ,它会为 'Yes' & 'Y' 返回 true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM