源代碼下載:?learnvisualbasic.vb-cn
Module Module1
Sub Main()
' 讓我們先從簡(jiǎn)單的終端程序?qū)W起。
' 單引號(hào)用來(lái)生成注釋(注意是半角單引號(hào),非全角單引號(hào)’)
' 為了方便運(yùn)行此示例代碼,我寫(xiě)了個(gè)目錄索引。
' 可能你還不了解以下代碼的意義,但隨著教程的深入,
' 你會(huì)漸漸理解其用法。
Console.Title = ("Learn X in Y Minutes")
Console.WriteLine("NAVIGATION") ' 顯示目錄
Console.WriteLine("")
Console.ForegroundColor = ConsoleColor.Green
Console.WriteLine("1\. Hello World Output") ' Hello world 輸出示例
Console.WriteLine("2\. Hello World Input") ' Hello world 輸入示例
Console.WriteLine("3\. Calculating Whole Numbers") ' 求整數(shù)之和
Console.WriteLine("4\. Calculating Decimal Numbers") ' 求小數(shù)之和
Console.WriteLine("5\. Working Calculator") ' 計(jì)算器
Console.WriteLine("6\. Using Do While Loops") ' 使用 Do While 循環(huán)
Console.WriteLine("7\. Using For While Loops") ' 使用 For While 循環(huán)
Console.WriteLine("8\. Conditional Statements") ' 條件語(yǔ)句
Console.WriteLine("9\. Select A Drink") ' 選飲料
Console.WriteLine("50\. About") ' 關(guān)于
Console.WriteLine("Please Choose A Number From The Above List")
Dim selection As String = Console.ReadLine
Select Case selection
Case "1" ' Hello world 輸出示例
Console.Clear() ' 清空屏幕
HelloWorldOutput() ' 調(diào)用程序塊
Case "2" ' Hello world 輸入示例
Console.Clear()
HelloWorldInput()
Case "3" ' 求整數(shù)之和
Console.Clear()
CalculatingWholeNumbers()
Case "4" ' 求小數(shù)之和
Console.Clear()
CalculatingDecimalNumbers()
Case "5" ' 計(jì)算器
Console.Clear()
WorkingCalculator()
Case "6" ' 使用 do while 循環(huán)
Console.Clear()
UsingDoWhileLoops()
Case "7" ' 使用 for while 循環(huán)
Console.Clear()
UsingForLoops()
Case "8" ' 條件語(yǔ)句
Console.Clear()
ConditionalStatement()
Case "9" ' If/Else 條件語(yǔ)句
Console.Clear()
IfElseStatement() ' 選飲料
Case "50" ' 關(guān)于本程序和作者
Console.Clear()
Console.Title = ("Learn X in Y Minutes :: About")
MsgBox("This tutorial is by Brian Martin (@BrianMartinn")
Console.Clear()
Main()
Console.ReadLine()
End Select
End Sub
' 一、對(duì)應(yīng)程序目錄1,下同
' 使用 private subs 聲明函數(shù)。
Private Sub HelloWorldOutput()
' 程序名
Console.Title = "Hello World Ouput | Learn X in Y Minutes"
' 使用 Console.Write("") 或者 Console.WriteLine("") 來(lái)輸出文本到屏幕上
' 對(duì)應(yīng)的 Console.Read() 或 Console.Readline() 用來(lái)讀取鍵盤(pán)輸入
Console.WriteLine("Hello World")
Console.ReadLine()
' Console.WriteLine()后加Console.ReadLine()是為了防止屏幕輸出信息一閃而過(guò)
' 類似平時(shí)常見(jiàn)的“單擊任意鍵繼續(xù)”的意思。
End Sub
' 二
Private Sub HelloWorldInput()
Console.Title = "Hello World YourName | Learn X in Y Minutes"
' 變量
' 用來(lái)存儲(chǔ)用戶輸入的數(shù)據(jù)
' 變量聲明以 Dim 開(kāi)始,結(jié)尾為 As VariableType (變量類型).
' 此教程中,我們希望知道你的姓名,并讓程序記錄并輸出。
Dim username As String
' 我們定義username使用字符串類型(String)來(lái)記錄用戶姓名。
Console.WriteLine("Hello, What is your name? ") ' 詢問(wèn)用戶輸入姓名
username = Console.ReadLine() ' 存儲(chǔ)用戶名到變量 username
Console.WriteLine("Hello " + username) ' 輸出將是 Hello + username
Console.ReadLine() ' 暫停屏幕并顯示以上輸出
' 以上程序?qū)⒃儐?wèn)你的姓名,并和你打招呼。
' 其它變量如整型(Integer)我們用整型來(lái)處理整數(shù)。
End Sub
' 三
Private Sub CalculatingWholeNumbers()
Console.Title = "Calculating Whole Numbers | Learn X in Y Minutes"
Console.Write("First number: ") ' 輸入一個(gè)整數(shù):1,2,50,104,等等
Dim a As Integer = Console.ReadLine()
Console.Write("Second number: ") ' 輸入第二個(gè)整數(shù)
Dim b As Integer = Console.ReadLine()
Dim c As Integer = a + b
Console.WriteLine(c)
Console.ReadLine()
' 以上程序?qū)蓚€(gè)整數(shù)相加
End Sub
' 四
Private Sub CalculatingDecimalNumbers()
Console.Title = "Calculating with Double | Learn X in Y Minutes"
' 當(dāng)然,我們還需要能夠處理小數(shù)。
' 只需要要將整型(Integer)改為小數(shù)(Double)類型即可。
' 輸入一個(gè)小數(shù): 1.2, 2.4, 50.1, 104.9,等等
Console.Write("First number: ")
Dim a As Double = Console.ReadLine
Console.Write("Second number: ") ' 輸入第二個(gè)數(shù)
Dim b As Double = Console.ReadLine
Dim c As Double = a + b
Console.WriteLine(c)
Console.ReadLine()
' 以上代碼能實(shí)現(xiàn)兩個(gè)小數(shù)相加
End Sub
' 五
Private Sub WorkingCalculator()
Console.Title = "The Working Calculator| Learn X in Y Minutes"
' 但是如果你希望有個(gè)能夠處理加減乘除的計(jì)算器呢?
' 只需將上面代碼復(fù)制粘帖即可。
Console.Write("First number: ") ' 輸入第一個(gè)數(shù)
Dim a As Double = Console.ReadLine
Console.Write("Second number: ") ' 輸入第二個(gè)數(shù)
Dim b As Integer = Console.ReadLine
Dim c As Integer = a + b
Dim d As Integer = a * b
Dim e As Integer = a - b
Dim f As Integer = a / b
' 通過(guò)以下代碼我們可以將以上所算的加減乘除結(jié)果輸出到屏幕上。
Console.Write(a.ToString() + " + " + b.ToString())
' 我們希望答案開(kāi)頭能有3個(gè)空格,可以使用String.PadLeft(3)方法。
Console.WriteLine(" = " + c.ToString.PadLeft(3))
Console.Write(a.ToString() + " * " + b.ToString())
Console.WriteLine(" = " + d.ToString.PadLeft(3))
Console.Write(a.ToString() + " - " + b.ToString())
Console.WriteLine(" = " + e.ToString.PadLeft(3))
Console.Write(a.ToString() + " / " + b.ToString())
Console.WriteLine(" = " + e.ToString.PadLeft(3))
Console.ReadLine()
End Sub
' 六
Private Sub UsingDoWhileLoops()
' 如同以上的代碼一樣
' 這次我們將詢問(wèn)用戶是否繼續(xù) (Yes or No?)
' 我們將使用Do While循環(huán),因?yàn)槲覀儾恢接脩羰欠裥枰褂靡淮我陨稀? Console.Title = "UsingDoWhileLoops | Learn X in Y Minutes"
Dim answer As String ' 我們使用字符串變量來(lái)存儲(chǔ)answer(答案)
Do ' 循環(huán)開(kāi)始
Console.Write("First number: ")
Dim a As Double = Console.ReadLine
Console.Write("Second number: ")
Dim b As Integer = Console.ReadLine
Dim c As Integer = a + b
Dim d As Integer = a * b
Dim e As Integer = a - b
Dim f As Integer = a / b
Console.Write(a.ToString() + " + " + b.ToString())
Console.WriteLine(" = " + c.ToString.PadLeft(3))
Console.Write(a.ToString() + " * " + b.ToString())
Console.WriteLine(" = " + d.ToString.PadLeft(3))
Console.Write(a.ToString() + " - " + b.ToString())
Console.WriteLine(" = " + e.ToString.PadLeft(3))
Console.Write(a.ToString() + " / " + b.ToString())
Console.WriteLine(" = " + e.ToString.PadLeft(3))
Console.ReadLine()
' 詢問(wèn)用戶是否繼續(xù),注意大小寫(xiě)。
Console.Write("Would you like to continue? (yes / no)")
' 程序讀入用戶輸入
answer = Console.ReadLine() ' added a bracket here
' 當(dāng)用戶輸入"yes"時(shí),程序?qū)⑻D(zhuǎn)到Do,并再次執(zhí)行
Loop While answer = "yes"
End Sub
' 七
Private Sub UsingForLoops()
' 有一些程序只需要運(yùn)行一次。
' 這個(gè)程序我們將實(shí)現(xiàn)從10倒數(shù)計(jì)數(shù).
Console.Title = "Using For Loops | Learn X in Y Minutes"
' 聲明變量和Step (步長(zhǎng),即遞減的速度,如-1,-2,-3等)。
For i As Integer = 10 To 0 Step -1
Console.WriteLine(i.ToString) ' 將計(jì)數(shù)結(jié)果輸出的屏幕
Next i ' 計(jì)算新的i值
Console.WriteLine("Start")
Console.ReadLine()
End Sub
' 八
Private Sub ConditionalStatement()
Console.Title = "Conditional Statements | Learn X in Y Minutes"
Dim userName As String = Console.ReadLine
Console.WriteLine("Hello, What is your name? ") ' 詢問(wèn)用戶姓名
userName = Console.ReadLine() ' 存儲(chǔ)用戶姓名
If userName = "Adam" Then
Console.WriteLine("Hello Adam")
Console.WriteLine("Thanks for creating this useful site")
Console.ReadLine()
Else
Console.WriteLine("Hello " + userName)
Console.WriteLine("Have you checked out www.learnxinyminutes.com")
Console.ReadLine() ' 程序停止,并輸出以上文本
End If
End Sub
' 九
Private Sub IfElseStatement()
Console.Title = "If / Else Statement | Learn X in Y Minutes"
' 有時(shí)候我們需要考慮多于兩種情況。
' 這時(shí)我們就需要使用If/ElesIf條件語(yǔ)句。
' If語(yǔ)句就好似個(gè)自動(dòng)售貨機(jī),當(dāng)用戶輸入A1,A2,A3,等去選擇物品時(shí),
' 所有的選擇可以合并到一個(gè)If語(yǔ)句中
Dim selection As String = Console.ReadLine() ' 讀入用戶選擇
Console.WriteLine("A1\. for 7Up") ' A1 七喜
Console.WriteLine("A2\. for Fanta") ' A2 芬達(dá)
Console.WriteLine("A3\. for Dr. Pepper") ' A3 胡椒醫(yī)生
Console.WriteLine("A4\. for Diet Coke") ' A4 無(wú)糖可樂(lè)
Console.ReadLine()
If selection = "A1" Then
Console.WriteLine("7up")
Console.ReadLine()
ElseIf selection = "A2" Then
Console.WriteLine("fanta")
Console.ReadLine()
ElseIf selection = "A3" Then
Console.WriteLine("dr. pepper")
Console.ReadLine()
ElseIf selection = "A4" Then
Console.WriteLine("diet coke")
Console.ReadLine()
Else
Console.WriteLine("Please select a product") ' 請(qǐng)選擇你需要的產(chǎn)品
Console.ReadLine()
End If
End Sub
End Module
我(譯注:原作者)在命令行下學(xué)習(xí)的VB。命令行編程使我能夠更好的了解程序編譯運(yùn)行機(jī)制,并使學(xué)習(xí)其它語(yǔ)言變得容易。
如果希望進(jìn)一步學(xué)習(xí)VB,這里還有更深層次的?VB教學(xué)(英文)。
所有代碼均通過(guò)測(cè)試。只需復(fù)制粘帖到Visual Basic中,并按F5運(yùn)行即可。
更多建議: