+ 1
Age Check If statement
Hello i've recently start that c# course and i have a problem with that if statement example int age = 18; if (age > 14) { if(age > 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Teenager"); } } else { if (age > 0) { Console.WriteLine("Child"); } else { Console.WriteLine("Something's wrong"); } } It outputs "Teenager", but why? I can't understand what is the logic. int age = 18; if (age > 14) Age is greater than 14, why it doesn't output "Adult"? English is not my native language, so if you see some typos or something - sorry : )
2 ответов
+ 2
age > 18
^That's your problem. The age is 18, so that isn't true because it isn't greater than 18. As such, it uses the else statement, which is "Teenager."
Change:
if(age > 18)
To:
if(age >= 18)
You'll want to do the same thing for your other IF checks, or you can alternatively just lower each number by 1. You're 0 years old until you're 1 years old, for example.
0
because you output Adult if age is greater than 18.
The conditional 'greater than or equal to' is >=
Also you can print adult if age is greater than 17