简体   繁体   English

Java中的布尔值和if / else语句帮助

[英]Boolean and if/else statement help in Java

I'm trying to have a user enter an input (their name and age, obviously) and if they're younger than 10 or older than 100, I want it to return back to the start and ask them for their age again, until the condition has been met, and then go on to ask the users name. 我试图让用户输入一个输入(显然是他们的姓名和年龄),如果他们小于10岁或大于100岁,我希望它返回到开始并再次询问他们的年龄,直到满足条件,然后继续询问用户名。 I know how to do that, a boolean. 我知道该怎么做,布尔值。 What I don't know how to do is to implement that into my if/else statements. 我不知道该怎么做是将其实现到我的if / else语句中。 Could anyone help me? 有人可以帮我吗?

public class Person {

    public static void main(final 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("How old are you really?");
        } else if ((age >= 18) && (age <= 100)) {
            System.out.println("So you're an adult, huh?");
        } else if (age > 100) {
            System.out.println("How old are you really?");
        }

        @SuppressWarnings("resource")
        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 + "?");

    }
}

Use a while loop while the condition is not met: while不满足条件的情况下使用while循环:

boolean conditionMet = false;
while(!conditionMet) {
    // ...
    else if (age >= 18 && age <= 100) {
        System.out.println("So you're an adult, huh?");
        conditionMet = true;     // the condition is met => exit the loop
    }
    // ...
}

Just put a while loop around your first scan and if statements. 只需在您的第一次扫描和if语句周围放置一个while循环。

boolean validAge = false;
while (!validAge) {
    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("How old are you really?");
    } else if ((age >= 18) && (age <= 100)) {
        System.out.println("So you're an adult, huh?");
        validAge = true;
    } else if (age > 100) {
        System.out.println("How old are you really?");
    }
}

You need a loop: 您需要一个循环:

boolean ageIsCorrect = false;
while (!ageIsCorrect) {
    // ask age, if the age is correct, set ageIsCorrect to true
}

将其放入while循环while(年龄介于指定的critiera之间)

Roughly something like this: 大概是这样的:

private static int  readAndCheck() {
    // read age and validate, upon failure return -1
}

public static void main(final String[] args) {

    int age = -1;
    while (age  == -1) { 
        age = readAndCheck();
    }

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

}

You should do that in a while loop. 您应该在while循环中这样做。 I think it's easier and better than other answers: 我认为它比其他答案更容易和更好:

int age = 0;
while (age<10 || age>100){
    age = scan.nextInt();
}
string username = scan.next();
...

Since you are looping, put the prompting code in a loop. 由于正在循环,因此将提示代码放入循环中。 Use a boolean to exit the loop 使用布尔值退出循环

boolean goodAnswer = false;
do
{
    System.out.println("Enter in your age.");
    ...
    if ((age >= 10) && (age  100)
    {
    ... // do not set goodAnswer, because the answer was not good
    }
} while (!goodAnswer);

Your code actually needs to be better encapsulated to check the age. 实际上,您的代码需要更好地封装以检查使用期限。

Use a method called isValidAge(age) to return a true or false result and then you can write: 使用名为isValidAge(age)的方法返回truefalse结果,然后可以编写:

int age = 0;

while(!isValidAge(age)){

System.out.println("Please try again! Not a valid age (for an adult)");

... read in the age using scanner...
}

Generally speaking if else () statements are messy and don't extend well - maintenance wise they are not always the best option. 一般来说,如果else()语句混乱且不能很好地扩展-从维护角度来看,它们并不总是最佳选择。

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

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