+ 2
Why the output is "Too young"?
Why is it 18 if a comparison is x < 18 and not x <= 18? Example: int age = 18; if (!(age < 18) { System.out.println("Too young"); } else { System.out.println("Welcome"); Thank you all!
6 Answers
+ 5
Rough code
Int age = 18;
If age==18
Print(exact)
Elif age>18
Print(welcome)
Else
Print(too young)
Try this way you don't get any error in the above code there is redundancy so it show wrong output
Hope it helps đđđđ
+ 5
I guess it's because you're using "!" . Your condition is if age is NOT larger than 18 then print out too young .
+ 4
Kristina Ray
18 < 18 = false
!false = true
So output would be "Too young"
18 <= 18 = true
!true = false
So output would be "Welcome"
+ 3
See the logical NOT operator before the expression ( age < 18 )? the logical NOT operator inverse the evaluation result. So here ( age < 18 ) is evaluated as true, but the logical NOT operator inverse the result (true) into (false).
P.S. Please add Java in post tags for language context clarity.
+ 2
The reason is very simple.
First,your condition age<18 is completely false.
But when you use ! Operator the condition becomes true.This is because the compiler only knows the value of age=18.
And that's the reason why you are getting 'Too young' as output.
0
Remove the sign "!".ok