Boolean and strings | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Boolean and strings

hey folks! i have this simple code Console.Write("Are you Cool?"); string str = Console.ReadLine(); if (str == "yes") { Console.WriteLine("HAHA! THAT'S NOT TRUE!"); } if (str == "no"){ Console.WriteLine("no? weeell.....i think you are"); } if (str != "yes" || "no") { Console.WriteLine("I'm Sorry, I Didn't understand you...doofus"); } how do i make this work? it says it does not work with || since bool values cannot have bool operands Could anyone help me with this? any help is really appreciated :D

2nd Feb 2017, 9:42 AM
Kenneth Bore Eilertsen
Kenneth Bore Eilertsen - avatar
9 Answers
+ 10
if(str != "yes" && str != "no") ....
2nd Feb 2017, 9:51 AM
Filip
Filip - avatar
+ 9
Because you are asking if both are true, if string is not "yes", and it is not "no" then print something
2nd Feb 2017, 9:59 AM
Filip
Filip - avatar
+ 9
Hm, you could write all the cases, for example if(str=="YES"||str=="Yes"||str=="yes")
2nd Feb 2017, 10:02 AM
Filip
Filip - avatar
+ 8
What @Filip said. Also you could do if, if else, else: Pseudo code: if(yes) ...HAHA else if(no) ...no? else ...doofus
2nd Feb 2017, 9:56 AM
Jafca
Jafca - avatar
+ 7
string str = Console.ReadLine().ToLower(); str is now all lower case so you only need to check with "yes" and "no".
2nd Feb 2017, 10:02 AM
Jafca
Jafca - avatar
+ 3
Easiest is regex but idk how it works in cs...
2nd Feb 2017, 10:06 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
Thanks! that worked! may i ask for an explanation of why AND works but not OR?
2nd Feb 2017, 9:58 AM
Kenneth Bore Eilertsen
Kenneth Bore Eilertsen - avatar
+ 2
is there also an easy way to make the code non-case sensetive? so that you can write ex: "YES", "Yes" or "yes"?
2nd Feb 2017, 10:01 AM
Kenneth Bore Eilertsen
Kenneth Bore Eilertsen - avatar
+ 1
All the things i asked about works now :D Filips "if(str != "yes" && str != "no")" gloriously solved my problem! Jafcas "string str = Console.ReadLine().ToLower();" made my code cooler! Thanks a bunch!
2nd Feb 2017, 10:11 AM
Kenneth Bore Eilertsen
Kenneth Bore Eilertsen - avatar