下表显示了VB.Net支持的所有算术运算符。假设变量A=2
,变量B=7
,则:
运算符 | 描述 | 说明 |
---|---|---|
^ |
一个操作数的指定次幂值 | B^A = 49 |
+ |
两个操作数相加 | A + B = 9 |
- |
第一个操作数减去第二个操作数 | A - B = -5 |
* |
两个操作数相乘 | A * B = 14 |
/ |
第一个操作数除以第二个操作数 | B / A = 3.5 |
\ |
第一个操作数除以第二个操作数的整数值 | B / A = 3 |
MOD |
模运算符,整数除法后的余数 | |
B / A = 1 |
示例
尝试下面的例子来理解VB.Net中的所有算术运算符:
文件:arithmetic_operators.vb
Module arithmetic_operators
Sub Main()
Dim a As Integer = 21
Dim b As Integer = 10
Dim p As Integer = 2
Dim c As Integer
Dim d As Single
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)
d = a / b
Console.WriteLine("Line 4 - Value of d is {0}", d)
c = a \ b
Console.WriteLine("Line 5 - Value of c is {0}", c)
c = a Mod b
Console.WriteLine("Line 6 - Value of c is {0}", c)
c = b ^ p
Console.WriteLine("Line 7 - Value of c is {0}", c)
Console.ReadLine()
End Sub
End Module
执行上面示例代码,得到以下结果 -
F:\worksp\vb.net\operators>vbc arithmetic_operators.vb
F:\worksp\vb.net\operators>arithmetic_operators.exe
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 d is 2.1
Line 5 - Value of c is 2
Line 6 - Value of c is 1
Line 7 - Value of c is 100
上一篇:
VB.Net运算符
下一篇:
VB.Net决策结构