0

我试图让用户输入一个输入(显然是他们的姓名和年龄),如果他们小于 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 + "?");

    }
}
4

8 回答 8

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
    }
    // ...
}
于 2013-04-04T13:10:34.473 回答
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?");
    }
}
于 2013-04-04T13:11:05.360 回答
0

你需要一个循环:

boolean ageIsCorrect = false;
while (!ageIsCorrect) {
    // ask age, if the age is correct, set ageIsCorrect to true
}
于 2013-04-04T13:10:06.633 回答
0

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

于 2013-04-04T13:10:16.777 回答
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 + "?");

}
于 2013-04-04T13:12:32.473 回答
0

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

int age = 0;
while (age<10 || age>100){
    age = scan.nextInt();
}
string username = scan.next();
...
于 2013-04-04T13:13:06.593 回答
0

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

boolean goodAnswer = false;
做
{
    System.out.println("请输入您的年龄。");
    ...
    if ((年龄 >= 10) && (年龄 100)
    {
    ... //不要设置goodAnswer,因为答案不好
    }
} 而 (!goodAnswer);
于 2013-04-04T13:13:15.700 回答
0

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

使用调用的方法isValidAge(age)返回 a trueorfalse结果,然后您可以编写:

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

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

于 2013-04-04T13:47:43.007 回答