in any case, this statement is recorded
if (sAwnser == ("hello" || "Hello" || "hi" || "Hi" || "hey" || "Hey" || "Hay" || "hey")) { }
an error
Operator '||' cannot be applied to operands of type 'string' and 'string'
if someone can help, he will be very grateful
To avoid so many comparisons you can do
var list = new string[] {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"}; if (list.Contains(answer)) { }
You must explicitly reference the first variable each time.
sAwnser == "hello"returns a boolean value. You cannot compare boolean to string.
sAwnser == "hello"
What you can do is create a collection and add all your individual lines to it. Subsequently, you can use .Contains()on it.
.Contains()
, ;
|| Operator (C# Reference)
- (||) - bool.
, string ||. boolean.
string
||
boolean
LINQ Enumerable.Any, :
Enumerable.Any
string[] array = new string[] {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"}; if (array.Any(sAwnser.Equals)) { // Your sAwnser is equal one of your array values. }
:
public static bool EqualsAny<T>(this T input, params T[] items) { return items.Contains(input); }
if (sAnswer.EqualsAny("hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey")) { ... }
if (sAwnser == "hello" || sAwnser == "Hello" || sAwnser == "hi" || sAwnser == "Hi" || sAwnser == "hey" || sAwnser =="Hey" || sAwnser =="Hay" || sAwnser =="hey")) { }
private bool CheckInput(String input) { String[] sAwnser = {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"}; for (int i = 0; i < sAwnser.Length; i++) { if (sAwnser[i].Equals(input)); return true; } return false; }
var sAnswer = "hello"; var answerList = new List<string> { "HELLO", "HI", "HEY", "HAY" }; bool checkAnswer = answerList.Any(r => r == sAnswer.ToUpper());
linq, , .ToUpper()
linq
.ToUpper()
. "hello" || "Hello" || etc.., || ( , ?)
"hello" || "Hello" || etc..
, , . , , - ?
if (someBool == (true || false)) something;
someBool
true
false
true || false
, ( ), , (, ) , , , . , , , , - .
.Equals()
String sAnswer = "hello"; if( sAnswer.Equals("hello")|| sAnswer.Equals("Hi")) System.Console.Write("yes"); else System.Console.Write("no");
, , ,
String sAnswer1 = "hello"; String sAnswer2 = "heLLo"; String sAnswer3 = "HellO"; sAnswer = sAnswer.ToUpper(); if( sAnswer1.Equals("HELLO")) -> True if( sAnswer2.Equals("HELLO")) -> True if( sAnswer3.Equals("HELLO")) -> True
, , contains():
String sAnswer = "watching"; if( sAnswer.Contains("hi"))
( watcHIng "" ), , ,
, :
if ((sAwnser == "hello") || (sAwnser =="Hello") || (sAwnser =="hi") || (sAwnser =="Hi") || (sAwnser =="hey") || (sAwnser =="Hey") || (sAwnser =="Hay") || (sAwnser =="hey")) { //insert code here }
, , , . , .
const , , :
private const string test = "HellohelloHihiHeyhey"; static void Main(string[] args) { string UserInput = Console.ReadLine(); if (test.Contains(UserInput)) { Console.WriteLine("success!!"); } }