+ 4
[Solved!] Problem with if statement returning else statement
Iâm writing this off of the if statement lesson for Java, and if you run it and put your age in as 14 or 15, it will return âToo youngâ but also âWelcome!â. How would I get it to not return Welcome on too young? https://code.sololearn.com/c8d1L51r9zk9/?ref=app
4 RĂ©ponses
+ 5
Its because both of your if which is <18 and ==18 are executed seperately
First the code run in <18 it's true so "too young" appear
Next the code run in ==18 its false because surely <18 is not same as 18, thats why welcome message appears
use else if to connect both the if
+ 3
Fixed! You were almost there đ Only thing you were missing was an âelseâ before the second âifâ. Look:
if (age < 18) {
System.out.println("Too Young");
}else if (age == 18) {
System.out.println("Just right");
}else {
System.out.println("Welcome!");
}
This solves yoir problem đđč
+ 2
Carlos J. Rodriguez that did it! thank you and thank you Taste đ
0
000 is the anwser