Can someone tell what's wrong with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone tell what's wrong with this code?

//Remove the given age below and enter your age. public class Program { public static void main(String[] args) { int age=19; if(age == 16) if(age == 17) { System.out.println("Get your provisional license"); if(age < 15){ System.out.println("Too young!"); if(age == 15){ System.out.println("Get learner's license"); if(age >= 18){ System.out.println("Congrats! Get driver's license."); } } } } } }

1st Mar 2021, 7:13 PM
Ava
Ava - avatar
2 Answers
+ 2
age=19 and if condition if(age==16) becomes false so it directly goes to else part but you dont have else. No other statements gets executed because all next statements is in if(age==16) { here.. } Hence there no output.. Learn about if-else structure for more understandings...
1st Mar 2021, 7:33 PM
Jayakrishna 🇮🇳
0
// instead use if () ... else if () ... else if ()
2nd Mar 2021, 9:09 PM
zemiak