Reputation: 47
I have this java code:
public class Ages {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value = 0;
String name;
int age;
System.out.print("Hey, what's your name? ");
name = keyboard.next();
System.out.println();
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
do{
if(age < 16){
System.out.println("You can't visit in the museum, " + name + ".");
if(age < 18);
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25){
System.out.println("You can do anything, " +
name + ".");
}
while(age > 300);
System.out.println("It's not correct..Try Again, " + name +
".");
}
}
and I need that user write the wrong answer, he will get the question again "how old are you?", and after he get another tries.. what I goona do? Thanks for the help!! :)
Upvotes: 2
Views: 704
Reputation: 131346
1)You should use this condition : if (age >= 25 && age <= 300){
instead of
if (age >= 25){
.
Otherwise the condition is true
even if age
is over 300 and you don't want to:
while(age > 300);
2)You should also allow the user to enter a new input if the value is not correct.
age = keyboard.nextInt();
should be in the loop.
3) You should allow the loop to finish.
In the while statement, you could specify the condition that requires a new input from the user :
while (age > 300)
This gives the code (not tested) :
int age;
do{
age = keyboard.nextInt();
if(age < 16){
System.out.println("You can't visit in the museum, " + name + ".");
}
else if(age < 18){
System.out.println("You can't visit in the park, " + name + ".");
}
else if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25 && age <= 300){
System.out.println("You can do anything, " +
name + ".");
}
else{
System.out.println("It's not correct..Try Again, " + name + ".");
}
}
while (age > 300);
Upvotes: 3
Reputation: 59960
One the while block is not correct you have to use :
System.out.println("It's not correct..Try Again, " + name + ".");
do {
if (age < 16) {
System.out.println("You can't visit in the museum, " + name + ".");
if (age < 18);
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25) {
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25) {
System.out.println("You can do anything, "
+ name + ".");
}
//while(age > 300);<<----------wrong position
System.out.println("It's not correct..Try Again, " + name + ".");
} while (age > 300);//<<----------correct position
Two
The while condition seams not correct, you have to check with min and max age, instead of just max, so you can use :
while (age < minAge || age > maxAge );
So if the age is less then the min OR great then the max repeat again
Three this if will not ever executed :
if (age < 18);//<<----------------note the ; it mean your if is end
System.out.println("You can't visit in the park, " + name + ".");
Four
Instead you can read the age inside your loop :
System.out.print("Hey, what's your name? ");
name = keyboard.next();
System.out.println();
do {
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
....
}while(..);
Hope this can gives you an idea about your problem.
Upvotes: 3
Reputation: 3346
Try this, please:
public class Ages {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value = 0;
int age;
System.out.print("Hey, what's your name? ");
String name = keyboard.next();
System.out.println();
while(true) {
System.out.print("Ok, " + name + ", how old are you? ");
age = keyboard.nextInt();
System.out.println();
if (age <= 300) {
break;
}
System.out.println("It's not correct..Try Again, " + name +
".");
}
if(age < 16) {
System.out.println("You can't visit in the museum, " + name + ".");
}
if(age < 18){
System.out.println("You can't visit in the park, " + name + ".");
}
if (age < 25){
System.out.println("You can't visit if you have a car, " + name + ".");
}
if (age >= 25){
System.out.println("You can do anything, " +
name + ".");
}
}
}
Upvotes: 1