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