+ 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 ответов
+ 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