【问题标题】:What does the '%' operator mean?'%' 运算符是什么意思?
【发布时间】:2011-03-16 22:11:20
【问题描述】:

我有下一个代码

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

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

【问题讨论】:

  • 取模或除法后的余数。
  • 请注意,该运算符几乎存在于所有语言中。
  • 是的,我愿意。但我可以像“%”运算符一样搜索它,而谷歌没有给出任何有用的页面。我不知道它叫“模数”
  • @Incognito:运营商通常不那么容易搜索...
  • @0xA3:在 Google 中搜索“C# 运算符”,% 位于弹出的第一页的第一行。

标签: c# operators


【解决方案1】:

modulo (or modulus) operator

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

例如:

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 的最大可能整数(更多详细信息在C# 4.0 规范7.8.3 余数运算符部分)。

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

Modulo operation(在维基百科上)

【讨论】:

    【解决方案2】:

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

    3 % 2 == 1
    789 % 10 = 9
    

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

    -1 % 2 == -1
    

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

    另见

    参考文献

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        它是模运算符。即 2 % 2 == 0, 4 % 4 % 2 == 0(2, 4 可被 2 整除,余数为 0), 5 % 2 == 1(2 进 5,余数为 1。)

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              【解决方案7】:

              它是模数,但你的例子不是很好地使用它。当两个整数相除时,它会为您提供余数。

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

              【讨论】:

                【解决方案8】:

                是模运算符

                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
                

                【讨论】:

                  【解决方案9】:

                  是几乎所有语言中都可用的基本运算符,通常称为模运算符。 结果是余数。

                  【讨论】:

                    【解决方案10】:

                    好吧,我确实知道这一点,直到只是尝试使用计算器并基本上玩弄:
                    5 % 2.2 = 0.6就像在计算器上说5/2.2 = 2.27然后你将.27乘以2.27然后你一轮,你得到0.6。希望这会有所帮助,它帮助了我 =]


                    【讨论】:

                      【解决方案11】:

                      这里没有人提供任何示例确切地如何方程可以返回不同的结果,例如比较 37/6 37%6,根据 Dirk Vollmar 的说法,在你们中的一些人认为自己做到了而感到不安之前,请暂停片刻并考虑一下,int x % int y 解析为 (x - (x / y) * y),这似乎相当乍一看很简单,但并非所有数学都以相同的顺序执行。

                      由于并非每个方程都有正确的括号,因此一些学校会教授将方程解析为((x - (x / y)) * y),而其他学校会教授(x - ((x / y) * y))

                      现在我尝试了我的示例 (37/6 & 37%6) 并找出了预期的选择(它是 (x - ((x / y) * y))),我什至展示了一个精心构建的 if 循环(尽管我忘记了每个 End线分号)以在最基本的尺度上模拟除法方程,因为这实际上是我的观点,方程是相似的,但根本不同。 这是我从已删除的帖子中记得的内容(我用手机打字花了大约一个小时,缩进是双倍行距的)

                      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();
                        }
                      }
                      

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

                      【讨论】:

                      • 我什至在精确值上使用了浮点数,因此您可以清楚地看到它在/ 上明确读取0.000,而% 清楚地读取1.000。我也意识到,一旦我最后发布了这个(37%6)=a,他们都是=1
                      • 如果代码有无效的操作数,我很乐意看到正确代码的建议你投反对票之前。
                      猜你喜欢
                      • 2013-05-07
                      • 2015-09-18
                      • 2017-03-29
                      • 2011-07-09
                      • 2016-07-23
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多