1 / 23

程式語言 -Visual Basic

程式語言 -Visual Basic. 條件結構. IF 條件結構. 標準架構 IF 條件敘述 THEN 程式碼 1 ELSE 程式碼 2 END IF. 範例 1. Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, Dim score As Single score = Val(TextBox1.Text) If score >= 60 Then Label2.Text = " 恭喜及格 "

lynsey
Download Presentation

程式語言 -Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 程式語言-Visual Basic 條件結構

  2. IF 條件結構 • 標準架構 IF 條件敘述 THEN程式碼1 ELSE程式碼2 ENDIF

  3. 範例1 • Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, Dim score As Single score = Val(TextBox1.Text) If score >= 60 Then Label2.Text = "恭喜及格" Else Label2.Text = "你死當了" End If End Sub • End Class

  4. 簡化架構 IF 條件敘述 Then 單行程式 (當條件結構中所要執行的程式只有一行時,可將該行程式置於Then之後,並可省略End IF) IF 條件敘述 Then 單行或多行程式 End IF

  5. IF 條件結構 • 擴充架構 IF 條件敘述 Then 程式碼(可多行) ElseIF 條件敘述 Then 程式碼(可多行) Else 程式碼(可多行) End IF 可以0組以上 可省略

  6. 巢狀式結構 • IF …. Then程式區塊1Else IF … Then程式區塊2 Else程式區塊3 End IF End IF

  7. 範例2 • Private Sub Button1_Click(ByVal sender As IF Val(TextBox1.Text) >=90 Then Label1.Text=“優等”ElseIF Val(TextBox1.Text) >=80 Then Label1.Text=“甲等” ElseIF Val(TextBox1.Text) >=70 Then Label1.Text=“乙等” Else Label1.Text=“丙等”End IFEnd Sub

  8. 範例3—控制項間的搭配 • 建立介面

  9. 撰寫核取方塊1(CheckBox1)程式Private Sub CheckBox1_CheckedChanged(ByVal ….. IF CheckBox1.Checked = True Then TextBox1.Enabled = True TextBox1.Focus() Else TextBox1.Enabled = False End IFEnd Sub • 說明: • CheckBox1_CheckedChanged 事件即為當核取方塊1勾選狀態被改變 • .Enable為設定控制項啟動作用(對文字框而言,True為接受資料輸入,反之不接受) • .Focus為設定游標至控制項 • 以同樣方式撰寫核取方塊2之程式

  10. 撰寫送出鈕(Button1)控制程式Private Sub Button1_Click(ByVal …… Dim tie, bag, sum as Integer tie = 0 : bag = 0 : sum = 0 IF CheckBox1.Checked = True Then tie = 50 * Val(Textbox1.Text) End IF IF CheckBox2.Checked = True Then bag = 150 * Val(Textbox2.Text) End IF sum = tie + bag MsgBox(“總計” & sum & “元” , , “結帳” )End Sub • 說明 • 冒號( :)可用於將指令串寫於同一行

  11. 撰寫取消鈕(Button2)控制程式Private Sub Button2_Click(ByVal …… CheckBox1.Checked = False CheckBox2.Checked = False TextBox1.Text = “” TextBox1.Enabled = False TextBox2.Text = “” TextBox2.Enabled = FalseEnd Sub

  12. 比較運算

  13. 資料的比較方式 • 當比較運算子兩邊都為數值資料時,數值大者為大 • 當比較運算子兩邊都為字串資料時,中文字>小寫英文字母>大寫英文字母>數字 • 當比較運算子兩邊都為日期資料時,越晚的日期越大

  14. 範例—比較輸入的資料大小 • Private Sub Button1_Click(ByVal sender …… • IF TextBox1.Text > TextBox2.Text Then • Label3.Text = "我比較大" • Else • IF TextBox1.Text = TextBox2.Text Then • Label3.Text = "哈,兩個一樣大" • Else • Label3.Text = "你比我大" • End If • End If • End Sub0

  15. 邏輯運算 • AND:只要有一邊False,結果就為False • True AND True →結果為True • True AND False →結果為False • 範例 • 3>1 AND 5>3 顯示結果為True • 4<2 AND 10<20 顯示結果為False

  16. OR:只要有一邊True,結果就為True • True OR False →結果為True • False OR False →結果為False • 範例 • 3>5 OR 5>1 結果為True • 1>2 OR 2>3 結果為False

  17. NOT:結果反向 • Not True →結果為False • 範例 • Not 3>1 結果為False • Not 5>10 結果為True

  18. 範例—電影分級 • Private Sub Button1_Click(ByVal sender • Dim age As Single • age = Val(TextBox1.Text) • If age < 6 And age > 0 Then • Label2.Text = "可以看普通級電影" • Else • If age < 12 And age >= 6 Then • Label2.Text = "可以看保護級電影" • Else • If age < 18 And age >= 12 Then • Label2.Text = "可以看輔導級電影" • Else • If age >= 18 Then • Label2.Text = "可以看限制級電影" • Else • Label2.Text = "哩喜來亂ㄟ喔" • End If • End If • End If • End If • End Sub

  19. 練習 • 設計一程式,輸入月份的天數 • 程式基本條件: • 一文字框,輸入1~12的月份。 • 一命令按鈕,按下後開始執行。 • 一標籤,顯示該月份的天數。 • 變數一律使用整數變數。

  20. Select Case 條件結構 • 主要特性:多重判斷 • 基本架構Select Case 資料或運算式Case 條件1程式區段1 Case 條件2程式區段2 Case 條件3程式區段3 Case Else程式區段End Select

  21. 範例—改寫電影分級 • Dim age As Integer age = Val(Textbox1.Text) Select Case age Case 1,2,3,4,5 Label1.Text=“可以看普通級電影”Case 6 To 11 Label1.Text=“可以看保護級電影”Case 12 To 17 Label1.Text=“可以看輔導級電影”Case IS >=18 Label1.Text=“可以看限制級電影”Case Else Label1.Text=“輸入年齡有問題”End Select

  22. 範例--亂數的使用 Public Class Form1 Private Sub Button1_Click(ByVal sender … Randomize() Label1.Text = Int(10 * Rnd()) End Sub End Class

  23. 練習 • 設計一猜數字遊戲。 • 程式基本條件: • 一Button按下則重新產生亂數 • TextBox輸入猜測的數字 • 一Button,按下後開始判斷。 • Label顯示結果。 • 產生一1~100之間的數字 a • 若輸入的數字大於產生的亂數,則於標籤顯示「太大了」,反之則顯示「太小了」,若一樣則顯示「答對了」。

More Related