940 likes | 1.21k Views
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
E N D
Visual BASIC 6.0 杜勇進
算術運算子 • +加 • -減 • *乘 • /實數除 5/2=2.5 • \整數除 5\2=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 • 假(FALSE) 0
資料型態 • 整數:INTEGER、LONG • 實數:SINGLE 、DOUBLE • 字串:STRING 、STRING*N • 日期 • 布林 • 可變 • 貨幣
變數宣告 • DIM A AS INTEGER • DIM A,B,C AS INTEGER • DIM X,Y AS SINGLE • DIM NAME AS STRING
變數宣告 • 一變數在記憶體佔一位置 • 明顯宣告OPTION EXPLICIT
符號 • ? 印出 • = 指定 • , 隔遠一點的印(14格) • ; 緊接著印 • : 合併多行敘述 • “…“字串 • ‘ 註解
輸出敘述(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
輸入窗 • inputstr=inputbox(提示[,標題][,預設值]) • ins=inputbox("輸入身高?") • ins=inputbox("輸入身高?","輸入個人資料") • ins=inputbox("輸入身高?","輸入個人資料","150") • ins必須宣告成字串
簡易輸入輸出 Option Explicit Dim INS As String Private SubForm_Activate() INS = InputBox("輸入身高?", "輸入個人資料", "150") Print "你的身高為 "; INS End Sub
轉換函數 • VAL() 字串變數值 • STR() 數值轉字串 • VAL("123") 123 • STR(456) "456"
簡易輸入輸出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
練習 • 輸入圓的半徑,輸出面積與周長 • 輸入二數,輸出和、差、積、商、餘數 • 輸入一數(三位數),輸出相反數字
IF敘述 • IF 條件式 THEN敘述 • IF條件式 THEN敘述ENDIF • IF條件式 THEN敘述ELSE敘述ENDIF
IF 範例 • 輸入一數求絕對值 (-3 3) • 輸入二數求最大數 (-50,10 10) • 輸入二數求絕對值最大數 (-50,1050) • 輸入一數判斷奇偶數 • 輸入三數求最大數 • 輸入四數求最大數 • 電影或等第分級制 • 輸入三數求由小至大印出
輸入一數求絕對值 (-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
輸入二數求最大數 (-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
SELECT CASE敘述 SELECT CASE資料或運算式 CASE 條件1 敘述1 CASE 條件2 敘述2 CASE 條件3 敘述3 CASE ELSE 敘述4 END SELECT
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
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
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
FOR 敘述 FOR 變數=初值TO 終值STEP 增量 敘述 敘述 NEXT 變數
S=1+2+...+10 S=0 FOR I=1 TO 10 S=S+I NEXT I PRINT S
P=1*2*3*...*10 P=1 FOR I=1 TO 10 P=P*I NEXT I
S=1+2+3+...+N N1=INPUTBOX("輸入一數") N=VAL(N1) S=0 FOR I=1 TO N S=S+I NEXT I PRINT S
S=1+3+5+...+99 S=0 FOR I=1 TO 99 STEP 2 S=S+I NEXT I PRINT S
S=5+10+15+...+90 S=0 FOR I=5 TO 90STEP 5 S=S+I NEXT I PRINT S
S=1+2+...+10 S=0 FOR I=10 TO 1 STEP -1 S=S+I NEXT I PRINT S
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
S=1+2+4+8+...+512 S=0 FOR I=0 TO 9 S=S+2^I NEXT I PRINT S
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
巢狀FOR S=0 FOR I =1 TO 10 FOR J =1 TO 10 S=S+1 NEXT J NEXT I PRINT S
巢狀FOR2 S=0 FOR I =1 TO 10 STEP 2 FOR J =1 TO 10 S=S+1 NEXT J NEXT I PRINT S
巢狀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
巢狀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
印圖案-1 FOR I=1 TO 5 ?"*"; NEXT I
印圖案-2 FOR I=1 TO 5 FOR J=1 TO I ?"*"; NEXT J PRINT '跳行 NEXT I * ** *** **** *****
印圖案-3 FOR I=1 TO 5 FOR J=1 TO I ?I; NEXT J PRINT '跳行 NEXT I 1 22 333 4444 55555
印圖案-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 * *** ***** ******* *********
印圖案-5 FOR I=1 TO 5 PRINT SPC(5-I); FOR J=1 TO 2*I-1 PRINT "*"; NEXT J PRINT NEXT I * *** ***** ******* *********
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)
陣列(Array) • 一群相同型別、固定大小的資料,共用一變數名稱,每一元素用註標來存取 • 註標須整數 • 有一維、二維、N維 • 宣告方式 • dim a(10) as integer • dim b(3,4) as single • dim name(10) as string
設定陣列初值 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
設定陣列初值(費氐係數) 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
設定陣列初值 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
產生亂數 • RND產生介於0~1之間亂數 • randomize 亂數種子
產生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