VB.Net - 算術運算子



下表顯示了 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
vb.net_operators.htm
廣告

© . All rights reserved.