Reputation: 355
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 + "?");
}
}
Upvotes: 0
Views: 3255
Reputation: 51
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.
Upvotes: 0
Reputation: 38290
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);
Upvotes: 0
Reputation: 667
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();
...
Upvotes: 0
Reputation: 6863
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 + "?");
}
Upvotes: 0
Reputation: 12243
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?");
}
}
Upvotes: 1
Reputation: 53809
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
}
// ...
}
Upvotes: 3
Reputation: 5362
Put it inside a while loop while(age is between your specified critiera)
Upvotes: 0
Reputation: 691635
You need a loop:
boolean ageIsCorrect = false;
while (!ageIsCorrect) {
// ask age, if the age is correct, set ageIsCorrect to true
}
Upvotes: 0