VB.Net - 算術(shù)運(yùn)算符

2022-06-02 09:20 更新

下表顯示了VB.Net支持的所有算術(shù)運(yùn)算符。 假設(shè)變量A為2,變量B為7,則:

運(yùn)算符描述示例
^將一個(gè)操作數(shù)作為另一個(gè)操作數(shù)的指數(shù)進(jìn)行求冪運(yùn)算B^A 的結(jié)果是 49
+兩個(gè)操作數(shù)相加A + B的結(jié)果是 9
-求第一個(gè)操作數(shù)減去第二個(gè)操作數(shù)的差A - B 的結(jié)果為 -5
*將兩個(gè)操作數(shù)相乘A * B 的結(jié)果為 14
/求一個(gè)操作數(shù)除以另一個(gè)操作數(shù),并返回一個(gè)浮點(diǎn)結(jié)果B / A 的結(jié)果為 3.5
\將一個(gè)操作數(shù)除以另一個(gè)操作數(shù),并返回一個(gè)整數(shù)結(jié)果(整除)B \ A 的結(jié)果為 3
MOD模數(shù)運(yùn)算符和整數(shù)除法后的余數(shù)(取模)B MOD A 的結(jié)果為 1

示例:

請(qǐng)嘗試下面示例了解所有可用在 VB.Net 算術(shù)運(yùn)算符
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
當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
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

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)