[英]or in if-statement doesn't work
I tried the following , I want to get a message if the entered number isn't 1, 2 or 3 : 我尝试了以下操作,如果输入的数字不是1、2或3,我想得到一条消息:
Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (number != 1 || number != 2 || number != 3)
{
Console.WriteLine("wrong number!");
}
but also if my input is 1,2 or 3 I get the message , but why ? 但是如果我的输入是1,2或3,我也会收到消息,但是为什么呢? whats wrong ?
怎么了 ? :/ there is no error by visual studio.
:/ Visual Studio没有错误。
You should use AND
operator in that if statement 您应该在该if语句中使用
AND
运算符
if (number != 1 && number != 2 && number != 3)
{
Console.WriteLine("wrong number!");
}
or in this case you can use 或者在这种情况下,您可以使用
if (number <= 0 || number >= 4)
{
Console.WriteLine("wrong number!");
}
You should consider using && operator which will check for at least number does not belong to one of those 您应该考虑使用&&运算符,该运算符将检查至少一个不属于其中一个的数字
if (number != 1 && number != 2 && number != 3)
{
Console.WriteLine("wrong number!");
}
TL;DR : Use the &&
operator to check for all three, instead of just checking for any of the three. TL; DR : 使用
&&
运算符检查所有三个,而不仅仅是检查三个。
You're currently checking: if the number is either not one , or not two , or not three , then say "wrong number". 您目前正在检查:如果该号码是不是没有一个 ,或者不是两个 ,或不是三个 ,然后说“打错了”。
if (number != 1 || number != 2 || number != 3)
Take, for example, number = 1
. 以
number = 1
为例。 The code will first check whether number != 1
, and will return false. 该代码将首先检查
number != 1
,然后返回false。 However, since you're using an OR statement, it will check the next one. 但是,由于您使用的是OR语句,它将检查下一个。 What ends up happening is this:
最终发生的事情是这样的:
if (false || true || true)
Since you're using OR, C# checks to see if ANY of those are true
. 由于您使用OR,看到C#检查是否有这些都是
true
。 So this further simplifies to the following: 因此,这进一步简化为以下内容:
if (true)
Since a number can not be !=1
, !=2
, and !=3
at the same time, this will always have at least two true
s . 由于一个数字不能同时为
!=1
, !=2
和!=3
,因此它始终至少具有两个true
。 Therefore, if you use the OR operator, the expression will always return true 因此,如果使用OR运算符,则表达式将始终返回true
What you want is the &&
operator. 您想要的是
&&
运算符。
//if number is NOT 1 AND NOT 2 AND NOT 3
if (number != 1 && number != 2 && number != 3)
With the example above, this would simplify to this: 在上面的示例中,这可以简化为:
if (false && true && true)
Which goes further to: 更进一步:
if (false)
//wrong number
Which will skip the inside, because 1 is a right number . 这将跳过内部,因为1 是正确的数字 。 If 1, 2, and 3 are wrong numbers , just put an exclamation point before the entire block, as follows:
如果1、2和3是错误的数字 ,只需在整个块之前放置一个感叹号,如下所示:
if (!(number != 1 && number != 2 && number != 3))
If I take a look at your code 如果我看看你的代码
Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (number != 1 || number != 2 || number != 3)
{
Console.WriteLine("wrong number!");
}
lets say number is now equal to 1 if (1 != 1 || 1 != 2 || 1 != 3)
this will give the following using Truth tables if ( false || true || true )
this will then result in if (true)
假设现在
if (1 != 1 || 1 != 2 || 1 != 3)
数字等于1, if (1 != 1 || 1 != 2 || 1 != 3)
使用if ( false || true || true )
将使用Truth表给出以下内容: if (true)
For more information about truth tables you can visit http://kias.dyndns.org/comath/21.html but in short using the stroke 有关真值表的更多信息,您可以访问http://kias.dyndns.org/comath/21.html,但总之使用笔触
true || 真实|| true = true
真=真
true || 真实|| false = true
假=真
false || 假|| true = true
真=真
false || 假|| false = false
假=假
But if you look at using the ampersand true && true = true 但是,如果您考虑使用&号,则true && true = true
true && false = false 真&&假=假
false && false = false 假&&假=假
This is the reason why you will always get to line Console.WriteLine("wrong number!");
这就是为什么您总是要进入
Console.WriteLine("wrong number!");
because if you enter 1,2,3 you will get atleast 2 'True' values, you have a number of options that you can follow 因为如果您输入1,2,3,您将获得至少2个“真”值,因此您可以选择许多选项
1) if (number != 1 || number != 2 || number != 3)
changes to if (number != 1 && number != 2 && number != 3)
2) make an array for valid values int[] valid = new int[] {1,2,3}
Then test if the input is inside of the valid array using a for loop or the easy Linq method Contains(...) 1)
if (number != 1 || number != 2 || number != 3)
变为if (number != 1 && number != 2 && number != 3)
2)为有效值int[] valid = new int[] {1,2,3}
]创建一个数组int[] valid = new int[] {1,2,3}
然后使用for循环或简单的Linq方法Contains(...)测试输入是否在有效数组内
Console.WriteLine("which number?");
int number = Convert.ToInt32(Console.ReadLine());
if (!valid.Contains(number))
{
Console.WriteLine("wrong number!");
}
Note I have added a Not operator infront of valid (!), this means if valid does not contain the value 注意我在有效(!)前面添加了一个Not运算符,这意味着如果有效不包含该值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.