运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。
运算符 | 描述 | 例 |
---|---|---|
+ | 加两个操作数 | A + B = 30 |
-- | 从第一个减去第二个操作数 | A-B = -10 |
* | 将两个操作数相乘 | A * B = 200 |
/ | 将分子除以除分子 | B / A = 2 |
% | 模运算符和整数除后的余数 | B%A = 0 |
++ | 增量运算符将整数值增加一 | A ++ = 11 |
- | 减法运算符将整数值减一 | A-- = 9 |
让我们看一个在C#中使用算术运算符的示例。
using System; namespace Demo { class Program { static void Main(string[] args) { int a = 99; int b = 33; int c; c = a + b; Console.WriteLine("Value of c is {0}", c); c = a - b; Console.WriteLine("Value of c is {0}", c); c = a * b; Console.WriteLine("Value of c is {0}", c); c = a / b; Console.WriteLine("Value of c is {0}", c); c = a % b; Console.WriteLine("Value of c is {0}", c); c = a++; Console.WriteLine("Value of c is {0}", c); c = a--; Console.WriteLine("Value of c is {0}", c); Console.ReadLine(); } } }
输出结果
Value of c is 132 Value of c is 66 Value of c is 3267 Value of c is 3 Value of c is 0 Value of c is 99 Value of c is 100