2

我在我的程序中创建是/否输入时遇到问题。我希望用户能够在最后一个打印语句之后输入 ay/n,但我不太确定如何实现它。我知道它很可能包含一个布尔值或 if/else 语句,但我不太确定。有什么帮助吗?

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");
        }
}
4

9 回答 9

10
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");
  }
}
于 2013-04-10T14:01:01.860 回答
3

这是你需要的吗?

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

或者,您可以使用更短的形式:

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

如果你的意思是别的,请发表澄清。

于 2013-04-10T14:05:52.400 回答
2

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

所以基本上,answer如果你输入任何以y. 否则为假。

于 2013-04-10T14:00:28.877 回答
0

您将读取一个字符串并将其与文字“y”进行比较。比较产生一个布尔值。

您可以将其扩展为首先检查它是“y”还是“n”并抱怨它是否不是,或者将其缩小以接受其他等价物,如“yes”和“no”。

无论如何,您的布尔值是通过与文字进行比较来实现的。

于 2013-04-10T13:59:15.443 回答
0

尝试这个,

boolean answer = "y".equalsIgnoreCase(in.nextLine());
于 2013-04-10T14:01:41.603 回答
0

你可以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"));
    }
}
于 2013-04-10T14:02:03.003 回答
0

我想通了。

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()));
    }
}
于 2017-07-16T21:31:15.133 回答
0

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

于 2020-12-16T20:39:48.963 回答
0

这是一个例子

 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;
            }
        }

  }
于 2019-12-04T13:51:46.133 回答