[英]Logical OR operator in string comparison?
Why does this comparison fails? 为什么此比较失败? How do I compare for alternating strings in csharp ?
如何比较csharp中的交替字符串?
Static void Main(string[] args)
{
string varFoo = "cat";
if (varFoo != "cat" || varFoo!="duck")
Console.WriteLine("You can enter.");
else
Console.WriteLine("Not allowed.");
Console.ReadLine();
}
Just wanted something as 只是想要一些东西
If(Either cat or a Duck)
// You're not allowed
else
// you are welcomed.
If I understand your comments, "cat" is allowed (and I suppose "duck" is also allowed). 如果我理解您的评论,则允许使用“ cat”(并且我认为也允许使用“ duck”)。
if (varFoo == "cat" || varFoo=="duck")
With your last edit (cat and duck can't enter). 上一次编辑(猫和鸭无法进入)。
if (varFoo != "cat" && varFoo !="duck")
this means : if varFoo == "cat"
: assertion will fail (left part is evaluated, as it is false, right part isn't evaluated => false). 这意味着:如果
varFoo == "cat"
:断言将失败(左侧部分被评估,因为它为false,右侧部分未被评估=> false)。
if varFoo == "duck"
: assertion will fail (left part is evaluated, it's true, then right part is evaluated, it's false => false) 如果
varFoo == "duck"
:断言将失败(对左侧部分进行评估,这是真的,然后对右侧部分进行评估,这是false => false)
this is just boolean way of life : 这只是布尔的生活方式:
true or false => true
true or true => true
true and false => false
false and true => false
One of your conditions is always true, no matter what the actual value is. 无论实际值是多少,您的条件之一始终是正确的。 You probably wanted a different comparison.
您可能想要一个不同的比较。
Considering the updated question, you'd be looking for the expression varFoo != "cat" && varFoo != "duck"
, as you obviously confused &&
(both conditions have to be true) and ||
考虑到更新后的问题,您将在寻找表达式
varFoo != "cat" && varFoo != "duck"
,因为您显然混淆了&&
(两个条件都必须为真)和||
(either condition have to be true). (任一条件都必须为真)。
From your comment 根据您的评论
Q: in what way does it fail?
问:它以什么方式失败? – dutzu
– dutzu
A: The cat is allowed ._.
A:允许猫。
i assume that you want to keap away cats and ducks. 我以为你想躲开猫和鸭。
You condition fails because of the Logical OR Operator ||
由于逻辑或运算符
||
您的条件失败 . 。 The second condition gets evaluated because
"cat" != "cat"
returns false but "cat" != "duck"
returns true. 第二个条件得到评估是因为
"cat" != "cat"
返回false,但是"cat" != "duck"
返回true。 That's why cats are also allowed to enter. 这就是为什么猫也被允许进入的原因。
You probably want to stop both from entering with 您可能想阻止两者都进入
if (varFoo != "cat" && varFoo != "duck")
Console.WriteLine("You can enter.");
else
Console.WriteLine("Not allowed.");
Update You last edit supports my opinion: 更新您上一次编辑支持我的意见:
Just wanted something as
只是想要一些东西
If(Either cat or a Duck)
// You're not allowed
Understand it differently: You define an action, this is to prevent some animals from entering. 以不同的方式理解它:您定义一个动作,这是为了防止某些动物进入。 You want to apply this action on
cats
and ducks
. 您想将此操作应用于
cats
和 ducks
。 You need an AND
instead of an OR
. 您需要
AND
而不是OR
。 (see above code) (请参见上面的代码)
Another approach which does the same is to define a collection of forbidden animals and use Contains
: 另一种相同的方法是定义一组禁忌动物并使用
Contains
:
IEnumerable<string> forbiddenAnimals = new List<string>(){ "cat", "duck" };
if(forbiddenAnimals.Contains(varFoo))
// You're not allowed
else
// you are welcomed
if (varFoo != "cat" || varFoo!="duck")
translation: 翻译:
varFoo is not equal to cat
OR varFoo is not equal to duck
. varFoo不等于
cat
或varFoo不等于duck
。 Since varFoo is not duck
; 由于varFoo不是
duck
; it returns true. 它返回true。
You probably want this: 您可能想要这样:
if (varfoo != "cat" && varfoo != "duck")
//You can enter.
With the || 与|| it will always fail, on all strings.
它会在所有字符串上始终失败。
if(varFoo != "cat" || varFoo != "duck")
so if( false || true )
false or true == true. if(varFoo != "cat" || varFoo != "duck")
所以if( false || true )
false或true == true。 it doesn't fail. 它不会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.