跳轉至內容

遊戲開發指南/程式語言/VB.NET/If 語句

75% developed
來自 Wikibooks,開放世界中的開放書籍
遊戲開發指南/程式語言/VB.NET
基本數學運算子和連線 If 語句 迴圈

If 語句

[編輯 | 編輯原始碼]

可以使用“if”語句檢查變數的值並將其與其他內容進行比較。以下是某些比較/條件運算子的列表

條件運算子 描述
= 等於
<> 不等於
> 大於
< 小於
>= 大於或等於
<= 小於或等於

If 語句使用示例

[編輯 | 編輯原始碼]
Dim Num1 as Integer = Console.ReadLine()
If Num1 = 5 Then
    Console.WriteLine("They chose the number 5!")
End If

'Stopping the program from closing
Console.ReadLine()

程式第一次執行

[編輯 | 編輯原始碼]

輸入

5

輸出

They have chose the number 5!

程式第二次執行

[編輯 | 編輯原始碼]

輸入

4

輸出

大於符號的使用示例

[編輯 | 編輯原始碼]
Dim Num1 as Integer = Console.ReadLine()
If Num1 > 5 Then
    Console.WriteLine("Their number was greater than 5!")
End If

'Stopping the console from closing
Console.ReadLine()

程式第一次執行

[編輯 | 編輯原始碼]

輸入

8

輸出

Their number was greater than 5!

程式第二次執行

[編輯 | 編輯原始碼]

輸入

3

輸出

程式第三次執行

[編輯 | 編輯原始碼]

輸入

5

輸出

請注意,如果它等於 5,則沒有輸出,要獲得 5 的輸出,程式碼可以更改為> 4>= 5

當 if 語句不為真時,使用此語句啟用。

Else 語句示例

[編輯 | 編輯原始碼]

示例程式碼

Dim Num1 as Integer = Console.ReadLine()
If Num1 >= 7 Then
    Console.WriteLine("Their number is greater than or equal to 7.")
Else
    Console.WriteLine("Their number is less than 7.")
End If

程式第一次執行

[編輯 | 編輯原始碼]

輸入

8

輸出

Their number is greater than or equal to 7.

程式第二次執行

[編輯 | 編輯原始碼]

輸入

7

輸出

Their number is greater than or equal to 7.

程式第三次執行

[編輯 | 編輯原始碼]

輸入

6

輸出

Their number is less than 7.

這些可以用來檢查除第一個 if 語句之外的其他內容。

Else If 語句示例

[編輯 | 編輯原始碼]
Dim Num1 as Integer = Console.ReadLine()
If Num1 = 8 Then
    Console.Writeline("Equal to 8.")
Else If Num1 >= 7 Then
    Console.WriteLine("7 and more.")
Else
    Console.WriteLine("Everything else (6 or less).")
End If

'Stopping the console from closing
Console.ReadLine()

程式第一次執行

[編輯 | 編輯原始碼]

輸入

9

輸出

7 and more.

程式第二次執行

[編輯 | 編輯原始碼]

輸入

7

輸出

7 and more.

程式第三次執行

[編輯 | 編輯原始碼]

輸入

6

輸出

Everything else (6 or less).

程式第四次執行

[編輯 | 編輯原始碼]

輸入

8

輸出

Equal to 8.

請注意,第一個被接受,但第二個語句仍然為真,但它沒有執行那部分程式碼,這是因為 if 語句從上到下執行,當它找到一個為真的語句時,它就不再檢查其他語句。

當兩者都為真時,使兩者都啟用的一種方法是執行以下操作

If Num1 = 8 Then
    Console.Writeline("Equal to 8.")
End If
If Num1 >= 7 Then
    Console.WriteLine("7 and more.")
Else
    Console.WriteLine("Everything else (6 or less).")
End If
此新演算法的輸入和輸出示例

[編輯 | 編輯原始碼]

輸入

8

輸出

Equal to 8.
7 and more.

您可以連續使用多個 Else If 語句

[編輯 | 編輯原始碼]

示例

Dim Num1 as Integer = Console.ReadLine()
If Num1 = 5 Then
    Console.WriteLine("Num1 is 5")
Else If Num1 = 6 Then
    Console.WriteLine("Num1 is 6")
Else If Num1 = 7 Then
    Console.WriteLine("Num1 is 7")
Else If Num1 = 8 Then
    Console.WriteLine("Num1 is 8")
Else
    Console.WriteLine("Num1 isn't 5, 6, 7 or 8")
End IF

'Stopping the console from closing
Console.ReadLine()

當然,這是一種低效的編碼方式,更好的方法是編寫

Console.WriteLine("Num1 is " & Num1)

但是上面的程式碼不允許使用 else 部分,為此,我們需要學習下面所示的布林邏輯運算子。

布林邏輯運算子

[編輯 | 編輯原始碼]

有 4 個主要的布林邏輯運算子可用:AND、OR 和 XOR、NOT。布林邏輯運算子比較其兩側的兩個布林語句,然後將其都轉換為另一個布林語句(NOT 除外,它隻影響其後的布林語句)。它們的定義如下

運算子 描述 真值表
And 只有當兩個語句都為真時才為真,否則為假。
左側輸入 右側輸入 輸出
Or 如果其中一個語句為真,或者兩個語句都為真,則為真。只有當兩個語句都為假時才為假。
左側輸入 右側輸入 輸出
Xor 當其中一個為真時為真。如果兩個都為真或兩個都為假,則為假(只有當它們不同時才為真)。
左側輸入 右側輸入 輸出
Not 反轉其後語句的布林值。
右側輸入 輸出

AND 示例

[編輯 | 編輯原始碼]

OR 示例

[編輯 | 編輯原始碼]

XOR 示例

[編輯 | 編輯原始碼]

NOT 示例

[編輯 | 編輯原始碼]

巢狀 If 語句

[編輯 | 編輯原始碼]
華夏公益教科書