1 / 92

Visual BASIC 6.0

Visual BASIC 6.0. 杜勇進. 即時視窗. 算術運算子. +加 -減 *乘 /實數除 5/2=2.5 整數除 52=2 MOD 餘數 18 mod 6 =0 ^ 次方 2^3=8. 關係運算子. <  < >  > =  = ≦  >= ≧  <= ≠  <>. 邏輯選算子. 及 AND 或 OR 不 NOT e.g. (5>1) AND (6<9) e.g. (A>B)  NOT(A<=B) e.g. (A>B) OR (X < Y) 真 (TRUE)  -1

remy
Télécharger la présentation

Visual BASIC 6.0

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 6.0 杜勇進

  2. 即時視窗

  3. 算術運算子 • +加 • -減 • *乘 • /實數除 5/2=2.5 • \整數除 5\2=2 • MOD餘數 18 mod 6 =0 • ^次方 2^3=8

  4. 關係運算子 • <  < • >  > • =  = • ≦  >= • ≧  <= • ≠  <>

  5. 邏輯選算子 • 及 AND • 或 OR • 不 NOT • e.g. (5>1) AND (6<9) • e.g. (A>B)  NOT(A<=B) • e.g. (A>B) OR (X < Y) • 真(TRUE)  -1 • 假(FALSE)  0

  6. 資料型態 • 整數:INTEGER、LONG • 實數:SINGLE 、DOUBLE • 字串:STRING 、STRING*N • 日期 • 布林 • 可變 • 貨幣

  7. 變數宣告 • DIM A AS INTEGER • DIM A,B,C AS INTEGER • DIM X,Y AS SINGLE • DIM NAME AS STRING

  8. 變數宣告 • 一變數在記憶體佔一位置 • 明顯宣告OPTION EXPLICIT

  9. 符號 • ? 印出 • = 指定 • , 隔遠一點的印(14格) • ; 緊接著印 • : 合併多行敘述 • “…“字串 • ‘ 註解

  10. 輸出敘述(Print) • print "Hello! I Love Visual basic!" • print a,b • print "sum=";a ; "total=";b • print "sum=";a , "total=";b △12△ △45△ △12△ 45 14

  11. 輸入窗 • inputstr=inputbox(提示[,標題][,預設值]) • ins=inputbox("輸入身高?") • ins=inputbox("輸入身高?","輸入個人資料") • ins=inputbox("輸入身高?","輸入個人資料","150") • ins必須宣告成字串

  12. 簡易輸入輸出 Option Explicit Dim INS As String Private SubForm_Activate() INS = InputBox("輸入身高?", "輸入個人資料", "150") Print "你的身高為 "; INS End Sub

  13. 轉換函數 • VAL() 字串變數值 • STR() 數值轉字串 • VAL("123") 123 • STR(456)  "456"

  14. 簡易輸入輸出2 Option Explicit Dim S1, S2, S3 As String Dim A, B, SUM As Integer Private Sub Form_Activate() S1 = InputBox("輸入第一個?", "兩數相加", "0") S2 = InputBox("輸入第二個?", "兩數相加", "0") A = Val(S1) B = Val(S2) SUM = A + B Print "兩數之和= "; SUM End Sub

  15. 練習 • 輸入圓的半徑,輸出面積與周長 • 輸入二數,輸出和、差、積、商、餘數 • 輸入一數(三位數),輸出相反數字

  16. IF敘述 • IF 條件式 THEN敘述 • IF條件式 THEN敘述ENDIF • IF條件式 THEN敘述ELSE敘述ENDIF

  17. IF 範例 • 輸入一數求絕對值 (-3  3) • 輸入二數求最大數 (-50,10  10) • 輸入二數求絕對值最大數 (-50,1050) • 輸入一數判斷奇偶數 • 輸入三數求最大數 • 輸入四數求最大數 • 電影或等第分級制 • 輸入三數求由小至大印出

  18. 輸入一數求絕對值 (-3  3) Option Explicit Dim S1 As String Dim A As Integer Private Sub Form_Activate() S1 = InputBox("輸入一個數?", "求絕對值", "0") A = Val(S1) If A < 0 Then A = -A End If Print "絕對值="; A End Sub

  19. 輸入二數求最大數 (-50,10  10) Option Explicit Dim S1, S2 As String Dim A, B, SUM As Integer Private Sub Form_Activate() S1 = InputBox("輸入第一個?", "兩數相加", "0") S2 = InputBox("輸入第二個?", "兩數相加", "0") A = Val(S1) B = Val(S2) If A > B Then Print "最大數="; A Else Print "最大數="; B End If End Sub

  20. SELECT CASE敘述 SELECT CASE資料或運算式 CASE 條件1 敘述1 CASE 條件2 敘述2 CASE 條件3 敘述3 CASE ELSE 敘述4 END SELECT

  21. SELECT CASE範例 A=8 : B=2 OP = "*" SELELCT CASE OP CASE "+" : C=A+B CASE "-" : C=A-B CASE "*" : C=A*B CASE ELSE C=-1 END SELECT

  22. SELECT CASE範例2 Option Explicit Dim S1, S2, OP As String Dim A, B, C As Integer Private Sub Form_Activate() S1 = InputBox("輸入第一個?", "求絕對值", "0") S2 = InputBox("輸入第一個?", "求絕對值", "0") A = Val(S1) : B = Val(S2) OP = "+" Select Case OP Case "+" : C = A + B Case "-" : C = A - B Case "*" : C = A * B Case Else : C = -1 End Select Print "結果="; C End Sub

  23. SELECT CASE範例3 Option Explicit Dim SCORE As Integer Dim S As String Private Sub Form_Activate() S = InputBox("輸入分數") SCORE = Val(S) Select Case SCORE Case 90 To 100 : Print "甲等" Case 80 To 89 : Print "乙等" Case 70 To 79 : Print "丙等" Case 60 To 69 : Print "丁等" Case Else : Print "不及格" End Select End Sub

  24. FOR 敘述 FOR 變數=初值TO 終值STEP 增量 敘述 敘述 NEXT 變數

  25. S=1+2+...+10 S=0 FOR I=1 TO 10 S=S+I NEXT I PRINT S

  26. P=1*2*3*...*10 P=1 FOR I=1 TO 10 P=P*I NEXT I

  27. S=1+2+3+...+N N1=INPUTBOX("輸入一數") N=VAL(N1) S=0 FOR I=1 TO N S=S+I NEXT I PRINT S

  28. S=1+3+5+...+99 S=0 FOR I=1 TO 99 STEP 2 S=S+I NEXT I PRINT S

  29. S=5+10+15+...+90 S=0 FOR I=5 TO 90STEP 5 S=S+I NEXT I PRINT S

  30. S=1+2+...+10 S=0 FOR I=10 TO 1 STEP -1 S=S+I NEXT I PRINT S

  31. S=1+2+4+8+...+512 S=0 : J=1 FOR I=1 TO 9 S=S+J J=J*2 NEXT I PRINT S

  32. S=1+2+4+8+...+512 S=0 FOR I=0 TO 9 S=S+2^I NEXT I PRINT S

  33. S=1+4+7+10+...+n(和<100最大n)

  34. S=1+2+...+N≦100 Option Explicit Dim N, S, I As Integer Private Sub Form_Activate() S = 0 For I = 1 To 100 If (S + I) < 100 Then S = S + I Else Exit For End If Next I Print "N="; I - 1, "S="; S End Sub

  35. 巢狀FOR S=0 FOR I =1 TO 10 FOR J =1 TO 10 S=S+1 NEXT J NEXT I PRINT S

  36. 巢狀FOR2 S=0 FOR I =1 TO 10 STEP 2 FOR J =1 TO 10 S=S+1 NEXT J NEXT I PRINT S

  37. 巢狀FOR3 S=0 FOR I =1 TO 10 FOR J =1 TO 10 IF (I >5) THEN EXIT FOR S=S+1 NEXT J NEXT I PRINT S

  38. 巢狀FOR4 S=0 FOR I =1 TO 10 IF (I MOD 2=) THEN FOR J =1 TO 10 IF (I >5) THEN EXIT FOR S=S+1 NEXT J ENDIF NEXT I PRINT S

  39. 印圖案-1 FOR I=1 TO 5 ?"*"; NEXT I

  40. 印圖案-2 FOR I=1 TO 5 FOR J=1 TO I ?"*"; NEXT J PRINT '跳行 NEXT I * ** *** **** *****

  41. 印圖案-3 FOR I=1 TO 5 FOR J=1 TO I ?I; NEXT J PRINT '跳行 NEXT I 1 22 333 4444 55555

  42. 印圖案-4 FOR I=1 TO 5 FOR J=1 TO 5-I '印空白 ?" "; NEXT J FOR J=1 TO 2*I-1'印星星 ?"*"; NEXT J PRINT '跳行 NEXT I * *** ***** ******* *********

  43. 印圖案-5 FOR I=1 TO 5 PRINT SPC(5-I); FOR J=1 TO 2*I-1 PRINT "*"; NEXT J PRINT NEXT I * *** ***** ******* *********

  44. FOR 作業 • 印圖案 • 印圖案 菱形 • 印圖案 菱形(中空) • 9*9乘法表 • 9*9乘法表(1,2,3) • 9*9乘法表(1,4,7) • S=1!+2!+3!+..+n! • S=1/2+2/3+3/4+....+N/(N+1)

  45. 陣列(Array) • 一群相同型別、固定大小的資料,共用一變數名稱,每一元素用註標來存取 • 註標須整數 • 有一維、二維、N維 • 宣告方式 • dim a(10) as integer • dim b(3,4) as single • dim name(10) as string

  46. 設定陣列初值 Option Explicit Dim a(10) As Integer Dim i, j As Integer Private Sub Form_Activate() For i = 1 To 10 a(i) = i '陣列放 1,2,3, ...,10 Next i For i = 1 To 10 Print a(i); Next i End Sub

  47. 設定陣列初值(費氐係數) Option Explicit Dim a(10) As Integer Dim i, j As Integer Private Sub Form_Activate() a(0)=0 : a(1)=1 For i = 2 To 10 a(i) = a(i-1)+a(i-2) Next i For i = 0 To 10 Print a(i); Next i End Sub

  48. 設定陣列初值 Option Explicit Dim b(3, 4) As Single Dim i, j As Integer Private Sub Form_Activate() For i = 1 To 3 For j = 1 To 4 b(i, j) =( i + j)/10 Next j Next i For i = 1 To 3 For j = 1 To 4 Print b(i, j); Next j Print Next i End Sub

  49. 產生亂數 • RND產生介於0~1之間亂數 • randomize 亂數種子

  50. 產生10亂數 Option Explicit Dim a(10) As Single Dim i, j As Integer Private Sub Form_Activate() For i = 1 To 10 a(i) = Rnd Next i For i = 1 To 10 Print a(i) Next i End Sub

More Related