Boolean and strings | Sololearn: Learn to code for FREE!


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

2/2/2017 9:42:36 AM

Kenneth Bore Eilertsen

9 Answers

New Answer


if(str != "yes" && str != "no") ....


Because you are asking if both are true, if string is not "yes", and it is not "no" then print something


Hm, you could write all the cases, for example if(str=="YES"||str=="Yes"||str=="yes")


What @Filip said. Also you could do if, if else, else: Pseudo code: if(yes) ...HAHA else if(no) else ...doofus


string str = Console.ReadLine().ToLower(); str is now all lower case so you only need to check with "yes" and "no".


Easiest is regex but idk how it works in cs...


Thanks! that worked! may i ask for an explanation of why AND works but not OR?


is there also an easy way to make the code non-case sensetive? so that you can write ex: "YES", "Yes" or "yes"?


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!