跳轉至內容

Visual Basic .NET/算術運算子

來自華夏公益教科書,開放的書籍,開放的世界

算術運算子

[編輯 | 編輯原始碼]

Visual Basic .NET 提供一組基本的運算子來計算簡單的算術運算。

+   Addition
-   Subtraction
*   Multiplication
/   Division
\   Integer division
Mod Remainder Division
^   Exponentiation
&   String concatenation

7 + 2     produces 9
7 - 2     produces 5
7 * 2     produces 14
7 / 2     produces 3.5
7 \ 2     produces 3
7 Mod 2   produces 1
7 ^ 2     produces 49
"7" & "7" produces "77"

在我們深入運算子本身之前,讓我們看一個算術運算的簡短示例。

在這個例子中,我們也會使用一些基本的變數。 Dim 運算子建立變數。

  Dim Commission As Single
  Dim Sales As Single
  Sales = 3142.51
  Commission = 0.3 * Sales  ' Calculate 30% commission.

首先,我們將總銷售額設定為 3142.51。

* 運算子計算乘法,因此最後一行等效於 。這意味著我們的第二步是將 0.3 和 Sales 相乘。Sales 是 3142.51,因此我們的結果應該是 0.3 和 3142.51 的乘積。

為什麼這些奇怪的符號?

[編輯 | 編輯原始碼]

除了加法和減法之外,使用的符號與現實生活中使用的符號不同。這僅僅是因為其他符號在標準鍵盤上不可用。

Windows 的內建計算器是 Visual Basic .NET 中運算子的一個很好的參考。如果將計算器設定為科學模式,就可以訪問與 .NET 中相同的運算子。


這將兩個數字加在一起,用 "+" 符號表示。如果涉及字串,它也可能進行字串連線。示例

  x = 7 + 2     ' Results in 9.
  x = 25 + -4   ' Results in 21.
  Dim StringA As String
  StringA = "A string" + "Another string" ' Results in "A stringAnother string"

使用奇怪的符號,在執行期間將很容易解析。大多數情況下,這就是他們使用這些符號的原因。

還有一個加法運算子 "+="。它將 += 左側的變數遞增 += 右側指示的量。

  Dim x As Integer = 54
  x += 89       ' result is 143
  x += 7       ' result is 150

它也作為連線運算子與字串一起使用。

  Dim x As String = "A fox"
  x += " jumped"          ' result is "A fox jumped"
  x += " over the fence"  ' result is "A fox jumped over the fence"

這將兩個數字相減,用 "-" 符號表示。示例

  Dim x As Double = 12.045
Dim Y As Integer= 12
  x = x-Y     ' Results in .045.
  x = 25 - -4  ' Results in 29.
    Sub Main()
        Dim x As Double = 12.045
        Dim y As Integer = 12

        System.Console.WriteLine(x - y)
        System.Console.ReadKey()

    End Sub

當從雙精度浮點數中減去整數時要小心。上面的程式碼在列印時,結果將是 .04499999999999999 而不是顯而易見的 .045。使用雙精度浮點數來儲存十進位制值可能非常不準確。

這將兩個數字相乘,用 "*" 符號表示。示例

  Dim x As Integer
  x = 7 * 2   ' Results in 14.
  x = 25 * -4 ' Results in -100.

除法比 "/" 符號表示的除法型別更多。還有整數除法和餘數除法。

除法
這是最常用的除法形式,用 "/" 運算子表示。示例
  Dim x As Single
  ' (note that we must use the Single class to have decimals)
  x = 7 / 2  ' Results in 3.5.
  x = 25 / 4 ' Results in 6.25.
整數除法
這將兩個數字相除,如果商是十進位制數,則給出不帶餘數的結果。示例
  Dim x As Integer
  x = 7 \ 2    ' Results in 3.
  x = 25 \ 4   ' Results in 6.
餘數除法
這將兩個數字相除,如果商是十進位制數,則給出結果的餘數。這用 "Mod" 運算子表示。示例
  Dim x As Integer
  x = 7 Mod 2  ' Results in 1.
  x = 25 Mod 4 ' Results in 1.

這將一個數字提升到一個冪。例如, 在 VB .Net 程式碼中是

  Dim x As Integer
  x = 7 ^ 2   ' Results in 49.

這將導致數字 49 被分配給變數 x。它也可以用來計算一個數字的平方根。一個數字的平方根是指這個數字被提升到 0.5 的冪。

  Dim x As Single
  x = 7 ^ 0.5 ' Results in a number around 2.645.

注意:必須確保變數被正確宣告才能獲得期望的結果。以下示例可以正常工作,但會產生錯誤的結果。這是因為 Integer 類不允許小數位(就像數學整數一樣)。

  Dim x As Integer
  x = 7 ^ 0.5 ' Results in 3.

由於 x 被宣告為 Integer 型別,因此平方根的值(一個實數)儲存不正確。

任何數字的 n 次根都可以透過將該數字提升到 的冪來計算。

  Dim x As Single
  Dim n As Single
  n = 7
  x = 2 ^ (1 / n)

這是因為

  • Round():舍入到最接近的整數。
  • Floor():舍入到較小的整數。
  • Ceiling():舍入到較大的整數。
  • Truncate():截斷小數位。
Module Module1
    Sub Main()
        Dim Number1 As Single = 1.5
        Console.WriteLine(Math.Round(Number1))  ' 2
        Console.WriteLine(Math.Floor(Number1))  ' 1
        Console.WriteLine(Math.Ceiling(Number1))  ' 2
        Console.WriteLine(Math.Truncate(Number1))  ' 1
        Console.ReadLine()
    End Sub
End Module
  • Max()
  • Min()
Module Module1
    Sub Main()
        Console.WriteLine(Math.Max(3, 4))
        Console.ReadLine()  ' 4
    End Sub
End Module
華夏公益教科書