0

What is the output?

import java.util.Scanner; public class Program { public static void main(String[] args) { int age = 30 if(age < 15){ System.out.println("Child!"); }else if(age >= 15 && age <= 18){ System.out.println("Young!"); }else if(age >= 19 && age < 30){ System.out.println("Adult!"); }else if(age >= 31 && age <= 50){ System.out.println("Mr./Mrs."); }else{ System.out.println("Old!"); } } }

1st Mar 2019, 12:32 PM
Narushipudenn
Narushipudenn - avatar
4 Answers
+ 5
age (30) is not less the 15 so the if statement is not executed. age is more than 15 but is not less than 30 so the first else if statement is not executed. age is more than 19 but is not less than 30 so the second else if statement is not executed. age is not more or equal to 31 so the third if statement is not executed. So the else statement is executed and "Old!" is outputted.
1st Mar 2019, 12:50 PM
LynTon
LynTon - avatar
+ 3
For a correct output you should have included age<=30 instead of age<30
1st Mar 2019, 1:30 PM
marjel101
marjel101 - avatar
+ 1
You should have just used the code playground
1st Mar 2019, 3:34 PM
Ore
Ore - avatar
+ 1
Returns "Old!" because no one condition appropriates to 30.
2nd Mar 2019, 10:06 PM
spkruglov