+ 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
9 Respostas
+ 10
if(str != "yes" && str != "no")
....
+ 9
Because you are asking if both are true, if string is not "yes", and it is not "no" then print something
+ 9
Hm, you could write all the cases, for example if(str=="YES"||str=="Yes"||str=="yes")
+ 8
What @Filip said.
Also you could do if, if else, else:
Pseudo code:
if(yes)
...HAHA
else if(no)
...no?
else
...doofus
+ 7
string str = Console.ReadLine().ToLower();
str is now all lower case so you only need to check with "yes" and "no".
+ 3
Easiest is regex but idk how it works in cs...
+ 2
Thanks! that worked! may i ask for an explanation of why AND works but not OR?
+ 2
is there also an easy way to make the code non-case sensetive? so that you can write ex: "YES", "Yes" or "yes"?
+ 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!