+ 1

what's wrong with this code? someone help me to understand it.

if(!(age > 18)) mean "if age is NOT greater than" but in this code age is greater than 18 so it should print too young, but why this is printing "welcome"? public class Program { public static void main(String[] args) { int age = 25; if(!(age > 18)) { System.out.println("Too Young"); } else { System.out.println("Welcome"); } } }

21st Apr 2018, 7:01 AM
RH Tasin
RH Tasin - avatar
8 ответов
+ 3
The code is working properly. EDIT: In the code above, you stated that if age is NOT greater than 18, it should print "Too Young", else print "Welcome". That means if age is greater than or equal to 18, it should print "Welcome". Since age is 25, which is greater than 18, it should print "Welcome".
21st Apr 2018, 7:09 AM
Ibaadi Jaya
Ibaadi Jaya - avatar
+ 3
(age < 18) is what you really want to do.
21st Apr 2018, 7:23 AM
Timon Paßlick
+ 3
Yes, I'm not stupid, but if you're 18, you're an adult, so it doesn't make sense.
21st Apr 2018, 9:05 AM
Timon Paßlick
+ 3
Donna I know.
21st Apr 2018, 9:08 AM
Timon Paßlick
+ 2
!(a>b) means a <= b, but you want a < b in this case because if you're 18, you're an adult.
21st Apr 2018, 9:12 AM
Timon Paßlick
+ 1
look properly, what I have typed, it's (!(age>18)) not (age>18)
21st Apr 2018, 9:00 AM
RH Tasin
RH Tasin - avatar
0
You (!) or not statememt says your looking for someone younger then 18. It cannot be true if age is 25
23rd Apr 2018, 8:12 PM
Luther Conley