代码之家  ›  专栏  ›  技术社区  ›  Polaris

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

  •  6
  • Polaris  · 技术社区  · 14 年前

    我有下一个密码

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

    11 回复  |  直到 14 年前
        1
  •  28
  •   Dirk Vollmar    14 年前

    它是 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 被抛出。

    如果 x 是非整数值 x % y 计算为 x – n * y ,在哪里 n 小于或等于的最大可能整数 x / y C#4.0规范 在节中 7.8.3余数运算符 ).

    有关更多详细信息和示例,请参阅相应的Wikipedia文章:

    Modulo operation (维基百科)

        2
  •  6
  •   Justin Niessner    14 年前

    这就是模运算符。它将给你除法运算的剩余部分。

        3
  •  5
  •   polygenelubricants    14 年前

    %

    3 % 2 == 1
    789 % 10 = 9
    

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

    -1 % 2 == -1
    

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

    另请参见

        4
  •  3
  •   Frank Shearar    14 年前

    这是模算符。也就是说,2%2==0,4%4%2==0(2,4可被2整除,余数为0),5%2==1(2化成5,余数为1)

        5
  •  2
  •   Tomas Aschan    14 年前

    它是 modulo 1 % 36 == 1 (0余数1)

        6
  •  1
  •   Mark Bell    14 年前

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

    所以在这种情况下 a 将是 b 除以 c

        7
  •  1
  •   Coding Flow    14 年前

    它是模数,但你的例子不能很好地使用它。当两个整数被除时,它给出余数。

    a = 7 % 3

        8
  •  0
  •   Å¡ljaker    14 年前

    它是模算子

    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
  •  0
  •   Kinjal    14 年前

        10
  •  0
  •   Santhosh    7 年前


    5 % 2.2 = 0.6 就像在计算器上说 5/2.2 = 2.27 .27 乘以 2.27 你转过来 0.6 . 希望这有帮助,它帮助了我=]


        11
  •  -3
  •   Blue64    7 年前

    这里没有人提供任何证据 当然 怎样 37/6 37%6 ,在你们中的一些人感到不安之前,停下来想一分钟,根据德克·沃尔马尔在这里的说法 int x % int y 解析为 (x - (x / y) * y) 乍一看,这似乎相当简单,但并不是所有的数学都是按同样的顺序进行的。

    ((x - (x / y)) * y) 在其他学校教书的时候 (x - ((x / y) * y))

    现在我用我的例子做实验( )并找出了选择的目的(它是 (x-((x/y)*y)) if 循环(尽管我忘记了每一个分号的结尾)来模拟最基本尺度上的除法方程,事实上这是我的观点,这个方程是相似的,但本质上是不同的。 以下是我从被删除的帖子中所记得的(我花了大约一个小时在手机上打字,缩进是双倍行距的)

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

    这也是 很明显 演示了对于完全相同的等式,结果是如何不同的。