+ 2
why is this true? Very thankful for any help :)
int age; bool can_drink; Console.Write("Enter your age: "); age=Console.Read(); can_drink=(age>=21)? true:false; Console.Write(can_drink); input(3) Output: true :/
12 Réponses
+ 5
I get it now, thank you both :)
+ 4
Thank you all, especially Sachin! Why does the conversion affect the output?
+ 4
To David, when I entered 22 it was still true. I even entered negative numbers, and it still evaluated to true. I tried Sachin's method, worked like a charm :), though I don't know why
+ 3
There is, I was just playing around with ternary if :)
+ 3
I somewhere read that, only the use of-
Console.Read() ot Console.ReadLine() takes input as a string, but here you need integer, that's why I convert the input into integer using-
Convert.ToInt32()
+ 3
:)
+ 2
Try this-
int age;
bool can_drink;
Console.Write("Enter your age: ");
age= Convert.ToInt32(Console.ReadLine());
can_drink=(age>=21)?true:false;
Console.Write(can_drink);
0
its there no sort of "if" statements for c#?
0
oh... what happends if you input is 22?
0
ok
0
bro sinahtoowart Upright
0
To all the people who are confused as to why that fixes the problem. I THINK it's because ReadLine() returns a string. So you have to convert/parse that string to get an int out of it.