Show 例子
优质
小牛编辑
124浏览
2023-12-01
下表显示了C#支持的所有算术运算符。 假设变量A保持10,变量B保持20,则 -
操作者 | 描述 | 例 |
---|---|---|
+ | 添加两个操作数 | A + B = 30 |
- | 从第一个减去第二个操作数 | A - B = -10 |
* | 将两个操作数相乘 | A * B = 200 |
/ | Divides numerator by de-numerator | B/A = 2 |
% | 模数运算符和整数除法后的余数 | B%A = 0 |
++ | 递增运算符将整数值增加1 | A ++ = 11 |
-- | 递减运算符将整数值减1 | A-- = 9 |
例子 (Example)
以下示例演示了C#中可用的所有算术运算符 -
using System;
namespace OperatorsAppl {
class Program {
static void Main(string[] args) {
int a = 21;
int b = 10;
int c;
c = a + b;
Console.WriteLine("Line 1 - Value of c is {0}", c);
c = a - b;
Console.WriteLine("Line 2 - Value of c is {0}", c);
c = a * b;
Console.WriteLine("Line 3 - Value of c is {0}", c);
c = a/b;
Console.WriteLine("Line 4 - Value of c is {0}", c);
c = a % b;
Console.WriteLine("Line 5 - Value of c is {0}", c);
c = a++;
Console.WriteLine("Line 6 - Value of c is {0}", c);
c = a--;
Console.WriteLine("Line 7 - Value of c is {0}", c);
Console.ReadLine();
}
}
}
编译并执行上述代码时,会产生以下结果 -
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 22
Line 7 - Value of c is 20