
<p><p >在C#中,基本算术运算符包括以下内容 −</p><table class="table table-bordered"><thead><tr><th style="max-width:90%">运算符</p><p></th><th style="width: 83.4747%; text-align: center;">描述</p><p></th></tr></thead><tbody><tr><td style="width: 16.5253%; text-align: center;" valign="top">+</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">将两个操作数相加</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">-</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">从第一个操作数中减去第二个操作数</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">*</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">将两个操作数相乘</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">/</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">将分子除以分母</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">%</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">取模运算符和整数除法后的余数</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">++</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">递增运算符将整数值增加一</p><p></td></tr><tr><td style="width: 16.5253%; text-align: center;" valign="top">--</p><p></td><td style="width: 83.4747%; text-align: center;" valign="top">递减运算符将整数值减少一</p><p></td></tr></tbody></table><p >要进行加法运算,使用加法运算符 −</p>
num1 + num2;
同样地,它适用于减法、乘法、除法和其他运算符。
示例
让我们看一个完整的示例来学习如何在C#中实现算术运算符。
实时演示
using System;
namespace Sample {
class Demo {
static void Main(string[] args) {
int num1 = 50;
int num2 = 25;
int result;
result = num1 + num2;
Console.WriteLine("Value is {0}", result);
result = num1 - num2;
Console.WriteLine("Value is {0}", result);
result = num1 * num2;
Console.WriteLine("Value is {0}", result);
result = num1 / num2;
Console.WriteLine("Value is {0}", result);
result = num1 % num2;
Console.WriteLine("Value is {0}", result);
result = num1++;
Console.WriteLine("Value is {0}", result);
result = num1--;
Console.WriteLine("Value is {0}", result);
Console.ReadLine();
}
}
}输出
Value is 75
Value is 25
Value is 1250
Value is 2
Value is 0
Value is 50
Value is 51