‘%’运算符是什么意思?

我有下一个代码

int a,b,c; b=1; c=36; a=b%c; 

“%”运算符是什么意思?

它是模数(或模数)运算符

模数运算符(%)在将第一个操作数除以第二个操作数后计算余数。

例如:

 class Program { static void Main() { Console.WriteLine(5 % 2); // int Console.WriteLine(-5 % 2); // int Console.WriteLine(5.0 % 2.2); // double Console.WriteLine(5.0m % 2.2m); // decimal Console.WriteLine(-5.2 % 2.0); // double } } 

样本输出:

  1
 -1
 0.6
 0.6
 -1.2

请注意, %运算符的结果等于x – (x / y) * y ,如果y为零,则抛出DivideByZeroException

如果xy是非整数值,则x % y计算为x – n * y ,其中n是小于或等于x / y的最大可能整数(更多细节见第7.8节中的C#4.0规范 。 3剩余运营商 )。

有关更多详细信息和示例,您可能需要查看相应的Wikipedia文章:

模数运算 (在维基百科上)

那是Modulo运算符。 它将为您提供除法运算的剩余部分。

%是许多C语言中的余数运算符。

 3 % 2 == 1 789 % 10 = 9 

负数有点棘手。 在例如Java和C#中,结果与被除数具有相同的符号:

 -1 % 2 == -1 

在例如C ++中,这是实现定义的。

也可以看看

  • 维基百科/ Modulo操作

参考

  • MSDN / C#语言参考/%运算符

它是模数运算符。 也就是说,2%2 == 0,4%4%2 == 0(2,4可被2整除,0剩余),5%2 == 1(2进入5,其余为1)。

它是模运算符。 即它除法后的余数1 % 36 == 1 (0余数1)

这是模运算符,它找到一个数除以另一个数的余数。

所以在这种情况下, a将是b的余数除以c

它是模数,但你的例子不是很好用。 当两个整数被分割时,它会给出余数。

例如a = 7 % 3将返回1,因为7除以3是2而剩下1。

它是模数运算符

 using System; class Test { static void Main() { int a = 2; int b = 6; int c = 12; int d = 5; Console.WriteLine(b % a); Console.WriteLine(c % d); Console.Read(); } } 

输出:

 0 2 

是几乎所有语言都可用的基本运算符,通常称为模运算符。 它给出了剩余的结果。

好吧,我确实知道这一点,直到尝试一个计算器并且基本上这样玩:
5 % 2.2 = 0.6就像在计算器上说5/2.2 = 2.27然后你乘以5/2.2 = 2.27乘以5/2.2 = 2.27然后你得到0.6 。 希望这有帮助,它帮助我=]


这里没有人提供任何关于方程式如何能够返回不同结果的例子 ,例如比较37/637%6 ,并且在你们有些人因为你做了之前感到沮丧之前,暂停片刻并考虑一下根据Dirk Vollmar的说法, int x % int y解析为(x - (x / y) * y) ,乍一看似乎相当简单,但并非所有数学都以相同的顺序执行。

由于不是每个方程都有它的正确括号,一些学校会教导方程式被解析为((x - (x / y)) * y)而其他学校教导(x - ((x / y) * y))

现在我尝试了我的例子( 37/637%6 )并找出了预期的选择(它是(x - ((x / y) * y)) ),我甚至展示了一个很好的构建if循环(甚至虽然我忘记了每个行结束分号)来模拟最基本尺度上的除法方程,因为这实际上是我的观点,方程式是相似的,但基本上是不同的。 这是我从删除的post中记得的东西(这花了我一个小时从我的手机输入,缩进是双空格)

 using System; class Test { static void Main() { float exact0 = (37 / 6); //6.1666e∞ float exact1 = (37 % 6); //1 float exact2 = (37 - (37 / 6) * 6); //0 float exact3 = ((37 - (37 / 6)) * 6); //0 float exact4 = (37 - ((37 / 6) * 6)); //185 int a = 37; int b = 6; int c = 0; int d = a; int e = b; string Answer0 = ""; string Answer1 = ""; string Answer2 = ""; string Answer0Alt = ""; string Answer1Alt = ""; string Answer2Alt = ""; Console.WriteLine("37/6: " + exact0); Console.WriteLine("37%6: " + exact1); Console.WriteLine("(37 - (37 / 6) * 6): " + exact2); Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3); Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4); Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c + ", d: " + d + ", e: " + e); Console.WriteLine("Answer0: " + Answer0); Console.WriteLine("Answer0Alt: " + Answer0Alt); Console.WriteLine("Answer1: " + Answer1); Console.WriteLine("Answer0Alt: " + Answer1Alt); Console.WriteLine("Answer2: " + Answer2); Console.WriteLine("Answer2Alt: " + Answer2Alt); Console.WriteLine("Init Complete, starting Math..."); Loop { if (a !< b) { a - b; c +1;} if else (a = b) { a - b; c +1;} else { String Answer0 = c + "." + a; //6.1 //this is = to 37/6 in the fact that it equals 6.1 ((6*6=36)+1=37) or 6 remainder 1, //which according to my Calculator App is technically correct once you Round Down the .666e∞ //which has been stated as the default behavior of the C# / Operand //for completion sake I'll include the alternative answer for Round Up also String Answer0Alt = c + "." + (a + 1); //6.2 Console.WriteLine("Division Complete, Continuing..."); Break } } string Answer1 = ((d - (Answer0)) * e); //185.4 string Answer1Alt = ((d - (Answer0Alt​)) * e); // 184.8 string Answer2 = (d - ((Answer0) * e)); //0.4 string Answer2Alt = (d - ((Answer0Alt​) * e)); //-0.2 Console.WriteLine("Math Complete, Summarizing..."); Console.WriteLine("37/6: " + exact0); Console.WriteLine("37%6: " + exact1); Console.WriteLine("(37 - (37 / 6) * 6): " + exact2); Console.WriteLine("((37 - (37 / 6)) * 6): " + exact3); Console.WriteLine("(37 - ((37 / 6) * 6)): " + exact4); Console.WriteLine("Answer0: " + Answer0); Console.WriteLine("Answer0Alt: " + Answer0Alt); Console.WriteLine("Answer1: " + Answer1); Console.WriteLine("Answer0Alt: " + Answer1Alt); Console.WriteLine("Answer2: " + Answer2); Console.WriteLine("Answer2Alt: " + Answer2Alt); Console.Read(); } } 

这也清楚地certificate了完全相同的方程的结果如何不同。