Java中的Boolean和if / else语句帮助

时间:2013-04-04 13:06:18

标签: java

我正在尝试让用户输入输入(显然是他们的姓名和年龄),如果他们小于10岁或者超过100岁,我希望它返回到开始并询问他们的年龄再次,直到条件满足,然后继续询问用户名称。我知道如何做到这一点,一个布尔值。我不知道该怎么做是将它实现到我的if / else语句中。谁能帮助我?

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 + "?");

    }
}

8 个答案:

答案 0 :(得分:3)

在不满足条件时使用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
    }
    // ...
}

答案 1 :(得分:1)

只需在第一次扫描和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?");
    }
}

答案 2 :(得分:0)

你需要一个循环:

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

答案 3 :(得分:0)

把它放在while循环中 while(年龄在您指定的标准之间)

答案 4 :(得分:0)

大概是这样的:

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 + "?");

}

答案 5 :(得分:0)

您应该在while循环中执行此操作。我认为它比其他答案更容易和更好:

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

答案 6 :(得分:0)

由于您正在循环,因此将提示代码放在循环中。使用布尔值退出循环

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

答案 7 :(得分:0)

您的代码实际上需要更好地封装以检查年龄。

使用名为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...
}

一般来说,如果else()语句很乱并且不能很好地扩展 - 维护方面它们并不总是最佳选择。