问题描述:

is there anyway of fixing this statement

if (sAwnser == ("hello" || "Hello" || "hi" || "Hi" || "hey" || "Hey" || "Hay" || "hey"))

{

}

it comes up with the error

Operator '||' cannot be applied to operands of type 'string' and 'string'

if anyone can help it would be much appreciated

网友答案:

For avoiding so many comparisons you can do

var list = new string[] {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"};

if (list.Contains(answer))
{
}
网友答案:

You have to explicitly reference the first variable every time.

sAwnser == "hello" returns a boolean. You cannot compare a boolean to a string.

What you could do is create a collection and add all your separate strings to that. Afterwards you can use .Contains() on it.

网友答案:

First of all, I want to point the root of your problem;

From || Operator (C# Reference)

The conditional-OR operator (||) performs a logical-OR of its bool operands.

So you can't use string values with || operator. Both operands should be boolean value.

You can use LINQ with Enumerable.Any as well like;

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.
}
网友答案:

One possible syntax is to create an extension method as follows:

public static bool EqualsAny<T>(this T input, params T[] items)
{
    return items.Contains(input);
}

Then you can call like this:

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"))
{

}

or you can write a seperate function to do the check

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;
        }
网友答案:

You could also do:

var sAnswer = "hello";
var answerList = new List<string> { "HELLO", "HI", "HEY", "HAY" };
bool checkAnswer = answerList.Any(r => r == sAnswer.ToUpper()); 

using linq, and you could set the options to uppercase, and .ToUpper() the answer

网友答案:

Most programming languages don't work like that. You are actually asking it to compute "hello" || "Hello" || etc.. and the || operator can not be applied to strings (what would you want the result to be, anyway?)

That the expression appears as the right hand side of a comparison is irrelevant. Consider also what would happen if that wasn't the case - what would this mean?

if (someBool == (true || false))
    something;

Should that

  1. always execute, because someBool is always either true or false, or
  2. execute only if someBool is true, because true || false is true?

Every programming language I know of (in which this is applicable) chooses the second case, and that can be generalized to other constructs that look similar (such as your example) and even more generally, looking at any sub-expression in isolation is enough to determine what it does. Choosing the first way makes it so that you have to look at a whole expression before you can determine what any of its sub-expressions might means, because their meaning may depend on something external to them.

网友答案:

use .Equals() to compare strings

        String sAnswer = "hello";
        if( sAnswer.Equals("hello")|| sAnswer.Equals("Hi"))
            System.Console.Write("yes");
        else
             System.Console.Write("no");

if you don't want to do a case sensitive comparison you can uppercase you sAnswer, this way you don't have to compare with multiple variants of the same string

        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

Also, be careful if you use the contains() method:

        String sAnswer = "watching";
        if( sAnswer.Contains("hi"))

will return you true (because watcHIng contains "hi") and i don't think you want that

网友答案:

Actually what you can do here is:

if ((sAwnser == "hello") || (sAwnser =="Hello") || (sAwnser =="hi") || (sAwnser =="Hi") || (sAwnser =="hey") || (sAwnser =="Hey") || (sAwnser =="Hay") || (sAwnser =="hey"))
{
 //insert code here
}

Compared to other suggestions, this could be the easiest way to code it, but some might think that this isn't a good practice. Anyway, have fun coding.

网友答案:

You can also declare a const string with all those values and each time you need to check just call contains method:

private const string test = "HellohelloHihiHeyhey";

static void Main(string[] args)
{
    string UserInput = Console.ReadLine();

    if (test.Contains(UserInput))
    {
        Console.WriteLine("success!!");
    }   
}
相关阅读:
Top