VB.Net - 算术运算符

2022-06-02 09:20 更新

下表显示了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 MOD A 的结果为 1

示例:

请尝试下面示例了解所有可用在 VB.Net 算术运算符
Module 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
当上述代码被编译和执行时,它产生以下结果:
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

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号