New course! Every coder should learn Generative AI!
Try a free lesson+ 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 Answers
+ 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