简体   繁体   中英

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. 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. 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:

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.

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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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