我有以下代码:
“%” 运算符是什么意思?
int a,b,c;
b=1;
c=36;
a=b%c;
“%” 运算符是什么意思?
int a,b,c;
b=1;
c=36;
a=b%c;
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
循环(尽管我忘记了每个行末分号)来模拟最基本的除法方程,因为这实际上是我的重点,该方程式相似,但根本不同。
以下是我从已删除的帖子中记得的内容(我用手机打了大约一个小时,缩进是双倍间距)
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();
}
}
这也清楚地展示了相同的方程式可能会有不同的结果。
(37%6)=a
,最终它们都等于1
。 - Blue64