+ 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"); } } }
8 odpowiedzi
+ 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".
+ 3
(age < 18) is what you really want to do.
+ 3
Yes, I'm not stupid, but if you're 18, you're an adult, so it doesn't make sense.
+ 3
Donna I know.
+ 2
!(a>b) means a <= b, but you want a < b in this case because if you're 18, you're an adult.
+ 1
look properly, what I have typed,
it's
(!(age>18)) not (age>18)
0
You (!) or not statememt says your looking for someone younger then 18. It cannot be true if age is 25