我是初学者,我一直在尝试运行一个程序,打印从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 butcher和not写的except.
应该:
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)
你说的话:
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)
你真正需要的是:
if ((a%21) != 0) Console.WriteLine(a);
Run Code Online (Sandbox Code Playgroud)
说明:可被a和b整除的数字基本上是可被a和b的LCM 整除的数字.由于3和7是素数,因此您基本上在寻找不能被3*7整除的数字.
查看条件语句的真值表,您可以看到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的倍数的原因.
你想要什么:数字,那是