C# 语法练习(3): 运算符
2009-02-23 08:17:05 来源:WEB开发网基本: . () [] x++ x-- new typeof checked unchecked -> ::
一元: + - ! ~ ++x --x (T)x True False & sizeof
乘除: * / %
加减: + -
移位: << >>
关系: < > <= >= is as
相等: == !=
逻辑: & ^ |
条件: && ||
赋值: = += -= *= /= %= &= |= ^= <<= >>=
选择: ?: ??
其他: =>
整数 / 整数 = 整数
using System;
class MyClass
{
static void Main()
{
double d;
d = 14 / 4;
Console.WriteLine(d); //3
d = 14 / 4.0;
Console.WriteLine(d); //3.5
d = 14.0 / 4.0;
Console.WriteLine(d); //3.5
d = 14.0 / 4;
Console.WriteLine(d); //3.5
Console.WriteLine();
float f;
f = 14 / 4;
Console.WriteLine(f); //3
f = (float)(14.0 / 4.0); /* 默认返回 double, 因而需要转换 */
Console.WriteLine(f); //3.5
Console.WriteLine();
int i;
i = 14 / 4;
Console.WriteLine(i); //3
i = (int)(14.0 / 4.0);
Console.WriteLine(i); //3
i = 14 % 4;
Console.WriteLine(i); //2
Console.ReadKey();
}
}
更多精彩
赞助商链接