&&运算符的行为类似于|| 操作者

Orn*_*ein 28 .net c# logic

我是初学者,我一直在尝试运行一个程序,打印从1到N(用户输入)的所有数字,除了那些可以同时被3和7整除的数字.然而,我的代码所做的是它打印从1到N的数字,除了那些可被3或7整除的数字.我检查了一段时间,我不知道它为什么这样做.请向我解释我哪里出错了.

static void Main(string[] args)
{
    int n = 0;
    int a = 0;
    n = Convert.ToInt32(Console.ReadLine());
    while (a <= n)
    {
        a++;
        if (a % 3 != 0 && a % 7 != 0)
        {
            Console.WriteLine(a);
        }
    }
    Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)

当我扭转if语句的标牌==&&运营工作正常,但如果标志是!=它只是就像一个||运营商,这样混淆了我,甚至更多.问题最有可能出现在这种情况下,但我看不出它有什么问题.

Mat*_*son 95

"除了可以同时被3和7整除的数字"之外,可以按如下方式细分:

"divisible by 3 and 7 at the same time" 可表示为:

"(divisible by 3 and divisible by 7)"

"Except"可以表达为"Not".

所以你得到:

Not (divisible by 3 and divisible by 7)

"被3整除"是 (a % 3) == 0

"被7整除"是 (a % 7) == 0

赠送:

Not ( (a % 3) == 0 and (a % 7) == 0)

在C#中Not变得!and&&,所以你可以写了整个事情在C#为:

if (!((a % 3) == 0 && (a % 7) == 0))


与你的错误比较:

if (a % 3 != 0 && a % 7 != 0)

后者是不正确的,因为它意味着:

if (the number is not divisible by 3) and (the number is not divisible by 7).

即它意味着"Print the number if it is neither divisible by 3 nor divisible by 7",这意味着"don't print the number if it's divisible by 3 or 7".

要了解原因,首先考虑数字6:

6 is not divisible by 3? = false (because 6 *is* divisible by 3)
6 is not divisible by 7? = true (because 6 is *not* divisible by 7)
Run Code Online (Sandbox Code Playgroud)

所以这if false and true当然就是解决了这个问题false.

此结果也适用于可被3整除的任何其他数字,因此不会打印3可被3整除的数字.

现在考虑数字14:

14 is not divisible by 3? = true (because 14 is *not* divisible by 3)
14 is not divisible by 7? = false (because 14 *is* divisible by 7)
Run Code Online (Sandbox Code Playgroud)

所以这if true and false当然就是解决了这个问题false.

此结果也适用于可被7整除的任何其他数字,因此不会打印7可被7整除的数字.

希望你能明白为什么现在错了.如果没有,请考虑这个等效示例:


假设我们有四个人,Tom the Carpenter,Dick the Carpenter,Harry the the Butcher和Tom the Butcher.

这个问题等同于您提出的问题:

 Name every person who is (not called Tom and is not a Butcher)
Run Code Online (Sandbox Code Playgroud)

你应该能够看到这与询问相同:

Name every person except (anyone called Tom or anyone who is a Butcher)
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,答案都是Dick the Carpenter.

你应该问的问题是:

Name every person except (anyone called Tom who is also a butcher)
Run Code Online (Sandbox Code Playgroud)

答案是汤姆木匠,迪克木匠和哈利屠夫.


脚注:德摩根的法律

第二部法律规定:

"not (A or B)" is the same as "(not A) and (not B)"
Run Code Online (Sandbox Code Playgroud)

这相当于我上面的例子,其中:

Name every person except (anyone called Tom or anyone who is a Butcher)
Run Code Online (Sandbox Code Playgroud)

相当于:

Name every person who is (not called Tom and is not a Butcher)
Run Code Online (Sandbox Code Playgroud)

其中A是anyone called Tom和B是anyone who is a butchernot写的except.

  • 尽管我们已经对这一彻底的解释表示赞赏,但我相信,通过逻辑陈述(即De Morgan定律)来解释正在进行的理论,我的答案将会受益. (13认同)
  • @ Leon7C我认为这样的解释超出了答案范围.有人已经将维基文章与De Morgan的法律联系起来(尽管我担心OP可能过于复杂,至少在现阶段).我与Tom,Dick和Harry的例子旨在为OP的具体问题提供逻辑的基本介绍.但是,我会添加一个脚注. (2认同)

Ham*_*jam 73

你应该阅读德摩根的法律

"不(A和B)"与"(不是A)或(不是B)"相同

也,

"not(A或B)"与"(不是A)和(不是B)"相同.

a % 3 != 0 && a % 7 != 0a不能被3(a % 3 != 0)整除而不能被7(a % 7 != 0)整除时为真.因此,所有a可被37 整除的s (3,6,7,9,12,14,...)使得整个表达式都是错误的.你可以改写它!(a % 3 == 0 || a % 7 == 0)

  • 这应该是公认的答案 - 它实际上并不是关于运算符,而是布尔逻辑的概念,一般来说OP没有得到. (2认同)

use*_*016 9

应该:

if ( !(a % 3 == 0 && a % 7 == 0) )
{
    Console.WriteLine(a);
}
Run Code Online (Sandbox Code Playgroud)

它完全意味着:除了那些可以同时被3和7整除的数字之外的所有数字.

您还可以将其改写为:

if ( a % 3 != 0 || a % 7 != 0 )
{
    Console.WriteLine(a);
}
Run Code Online (Sandbox Code Playgroud)


Han*_*ant 9

你说的话:

   if not (divisible by 3 and divisible by 7) then print
Run Code Online (Sandbox Code Playgroud)

你写的:

   if not divisible by 3 and not divisible by 7 then print
Run Code Online (Sandbox Code Playgroud)

不一样的事情.亚里士多德首先想到了这一点,奥古斯都德摩根在158年前写了法律,将非运算符应用于操作数并反转逻辑运算:

   if not divisible by 3 or not divisible by 7 then print
Run Code Online (Sandbox Code Playgroud)

哪个产生:

   if (a % 3 != 0 || a % 7 != 0)
Run Code Online (Sandbox Code Playgroud)

或者按照你说的方式写它:

   if (!(a % 3 == 0 && a % 7 == 0))
Run Code Online (Sandbox Code Playgroud)


dis*_*ame 9

你真正需要的是:

if ((a%21) != 0) Console.WriteLine(a);
Run Code Online (Sandbox Code Playgroud)

说明:可被ab整除的数字基本上是可被abLCM 整除的数字.由于3和7是素数,因此您基本上在寻找不能被3*7整除的数字.


dis*_*ame 5

查看条件语句的真值表,您可以看到if

 X(NOT multiple of 3) Y(NOT multiple of 7)   X && Y
      true                   true            'a' printed as it is not a multiple of either
      true                   false           'a' not printed, it is multiple of 7
      false                  true            'a' not printed, it is multiple of 3
      false                  false           'a' not printed, it is multiple of both 
Run Code Online (Sandbox Code Playgroud)

这就是为什么不打印所有3或7或21的倍数的原因.


你想要什么:数字,那是

  • 不是一个(在3和7的倍数).那就是
  • !(a%3 == 0 && a%7 == 0)或甚至进一步简化为
  • !(a%21 == 0)或甚至
  • (a%21!= 0)