5.2.2 數(shù)組元素的輸出
例:一維數(shù)組的輸出
二維數(shù)組的輸出
Option Explicit
Option Base 1
Private Sub Form_click()
Dim k As Integer, i As Integer
Dim A(4, 5) As Integer, J As Integer, S As String
'生成數(shù)組
k = 10
For i = 1 To 4
If i Mod 2 <> 0 Then '處理奇數(shù)行
For J = 1 To 5
k = k + 1
A(i, J) = k
Next J
Else
For J = 5 To 1 Step -1 '處理偶數(shù)行
k = k + 1
A(i, J) = k
Next J
End If
Next i
'數(shù)組輸出
If Check1.Value <> 1 Then
For i = 1 To 4 '輸出到圖片框
For J = 1 To 5
Picture1.Print A(i, J);
Next J
Picture1.Print
Next i
Else
For i = 1 To 4 '輸出到多行文本框
For J = 1 To 5
S = S & Str(A(i, J))
Next J
S = S & Chr(13) & Chr(10)
Next i
Text1.Text = S
End If
End Sub
7.2.3 數(shù)組元素的引用
例5-1 P95
Option Explicit
Option Base 1
Private Sub Form_Click()
Dim Compare(12) As Integer, i As Integer
Dim Max As Integer, Min As Integer
Randomize
For i = 1 To 12
Compare(i) = Int(90 * Rnd) + 10
Print Compare(i);
Next i
Max = Compare(1): Min = Compare(1)
For i = 2 To 12
If Compare(i) > Max Then
Max = Compare(i)
ElseIf Compare(i) < Min Then
Min = Compare(i)
End If
Next i
Print " 最大數(shù)是:"; Max
Print " 最小數(shù)是:"; Min
End Sub
例5-7 求裴波拉契數(shù)列。
Option Base 1
Option Explicit
Private Sub Form_Click()
Dim Fb(18) As Integer, I As Integer
Fb(1) = 1: Fb(2) = 1
For I = 3 To 18
Fb(I) = Fb(I - 2) + Fb(I - 1)
Next I
For I = 1 To 18
Text1 = Text1 & Str(Fb(I)) & ""
Next I
End Sub
例5-8 :
Option Explicit
Private Sub Command1_ClicK()
Dim St As String, Idx As Integer
Dim A(0 To 25) As Integer
Dim I As Integer, js As Integer
Dim CH As String * 1, L As Integer
St = Text1.Text
L = Len(St)
For I = 1 To L
CH = Mid(St, I, 1)
If CH >= "A" And CH <= "Z" Then
Idx = Asc(CH) - Asc("A")
A(Idx) = A(Idx) + 1
ElseIf CH >= "a" And CH <= "z" Then
Idx = Asc(CH) - Asc("a")
A(Idx) = A(Idx) + 1
End If
Print A(Idx)
Next I
For I = 0 To 25
If A(I) <> 0 Then
js = js + 1
Text2 = Text2 & Chr(I + Asc("A")) & ":" & Str(A(I)) & ""
If js Mod 5 = 0 Then Text2 = Text2 & Chr(13) & Chr(10)
End If
Next I
End Sub
相關(guān)推薦:
2012年下半年計(jì)算機(jī)等級(jí)考試報(bào)名時(shí)間及入口匯總
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |