'%' 运算符是什么意思?

9
我有以下代码:
int a,b,c;
b=1;
c=36;
a=b%c;

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

5
取模运算,即除法后的余数。 - Mark H
3
请注意,这个运算符存在于几乎所有语言中。 - mathk
1
是的,我知道。但我可以使用“%”运算符进行搜索,而谷歌没有给出任何有用的页面。我不知道它被称为“模数”。 - Polaris
4
@隐身用户:运算符通常不太容易搜索到... - Dirk Vollmar
1
@0xA3:在Google中搜索“C#运算符”,%符号出现在弹出的第一页的第一行。 - Justin Ardini
显示剩余2条评论
11个回答

-3
这里没有人提供任何关于方程如何返回不同结果的例子,例如比较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循环(尽管我忘记了每个行末分号)来模拟最基本的除法方程,因为这实际上是我的重点,该方程式相似,但根本不同。 以下是我从已删除的帖子中记得的内容(我用手机打了大约一个小时,缩进是双倍间距)

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 - Blue64
1
如果代码有无效的操作数,我希望在你投票之前能看到正确代码的建议。 - Blue64

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接