【例5-1】 输入10个整数用数组保存起来,并求其中大于10的个数及平均值。
Private Sub Command1_Click()
Dim a(1 To 10) As Integer
Dim i As Integer,TheCount As Integer
Dim TheSum As Long
Dim TheAvg As Single
For i = 1 To 10 '用循环输入10个整数
a(i) = Val(InputBox("请输入第" & i & "个整数:"))
If a(i) > 10 Then '如果输入的整数满足要求则进行处理
TheSum = TheSum + a(i) '记录满足要求整数的和
TheCount = TheCount + 1 '记录满足要求整数的个数
End If
Next
TheAvg = TheSum / TheCount '计算满足要求整数的平均值
Print "您输入的数据是:";
For i = 1 To 10 '用循环输出10个整数
Print a(i);
Next
Print '换行输出
Print
Print "大于10的整数个数为:" & TheCount
Print
Print "大于10的整数的平均值为:" & TheAvg
End Sub
【例5-2】 按钮的Click事件过程中代码如下:
Private Sub Command1_Click()
Static a(1 To 5) As Integer
Dim i As Integer
For i = 1 To 5
a(i) = a(i) + 1
Print a(i);
Next
Print
End Sub
【例5-3】 假设已经定义了一个模块级的数组a(1 To 5,1 To 5)用来存储整数,并且数组中的元素已经赋值。求与数组相对应的矩阵的主对角线元素值的和。
Private Sub Command1_Click()
Dim i As Integer,j As Integer
Dim TheSum As Long
For i = 1 To 5
TheSum = TheSum + a(i,i)
Next
Print "主对角线元素的和为:" & TheSum
End Sub
【例5-4】 假设一个4×4的矩阵已经存储在模块级的数组a(3,3)中,求矩阵的转置。
Private Sub Command1_Click()
Dim i As Integer,j As Integer,t As Integer
Print "源矩阵为:"
For i = 0 To 3 '外层循环控制打印矩阵的行
For j = 0 To 3 '内层循环控制打印矩阵的列
Print a(i,j);
Next
Print '打印完一行后换行
Next
'外层循环控制行,因为只交换主对角线下的元素
'所以从第二行开始,第一行的下标为0
For i = 1 To 3
'内层循环控制列,因为只交换主对角线下的元素
'所以从第0列到第i-1列(第i列为主对角线)
For j = 0 To i - 1
t = a(i,j)
a(i,j) = a(j,i)
a(j,i) = t
Next
Next
Print "转置后矩阵为:"
For i = 0 To 3
For j = 0 To 3
Print a(i,j);
Next
Print
Next
End Sub
【例5-5】 执行下列语句:
Private Sub Command1_Click()
Dim a() As Integer,b() As Integer
ReDim a(1 To 2)
a(1) = 1
a(2) = 2
ReDim b(1 To 2)
b(1) = 1
b(2) = 2
ReDim a(1 To 3)
Print a(1); a(2); a(3)
ReDim Preserve b(1 To 3)
Print b(1); b(2); b(3)
End Sub
【例5-6】 输入正整数n,产生n个1~100之间的随机数,并保存在数组中。
Private Sub Command1_Click()
Dim i As Integer,n As Integer
Dim a() As Integer
n = Val(InputBox("请输入一个正整数:"))
ReDim a(1 To n)
For i = 1 To n
a(i) = Int(Rnd * 100) + 1
Next
End Sub
【例5-7】 清除数组a的内容。
Private Sub Command1_Click()
Dim a(1 To 5) As Integer
Dim i As Integer
For i = 1 To 5
a(i) = i
Next
Print "数组值为:"
For i = 1 To 5
Print a(i);
Next
Print
Erase a
Print "执行Erase语句后数组值为:"
For i = 1 To 5
Print a(i);
Next
End Sub
【例5-8】 假设已经给出了一个一维数组a,里面存放的都是整数。求数组a里所有奇数的和。
Private Sub Command1_Click()
Dim TheSum As Long
For Each m In a 'm是一个缺省声明的变体变量
If m Mod 2 = 1 Then
TheSum = TheSum + m
End If
Next
Print "奇数元素的和为:" & TheSum
End Sub
【例5-9】 建立含有4个单选按钮的控件数组,当单击某个单选按钮时,改变文本框中字体的大小。
Private Sub Option1_Click(Index As Integer)
Select Case Index '根据Index值来判断单击了哪个单选按钮
Case 0
Text1.FontSize = 10
Case 1
Text1.FontSize = 12
Case 2
Text1.FontSize = 14
Case 3
Text1.FontSize = 16
End Select
End Sub
【例5-10】 建立含有4个单选按钮的控件数组,当单击某个单选按钮时,改变文本框的文本为单选按钮的标题。
Private Sub Option1_Click(Index As Integer)
'利用Index属性引用控件数组中的元素
Text1.Text = Option1(Index).Caption
End Sub
【例5-11】 下面程序段求1~100的5的倍数或7的倍数的和。
Private Sub Form_Click()
Sum = 0
For i = 1 To 100
 If i Mod 5 = 0 Or i Mod 7 = 0 Then
Sum = Sum + i
 End If
Next i
Print Sum
End Sub
【例5-12】 求前N个自然数的每个数阶乘之和。
Private Sub Command1_Click()
Dim i As Integer,j As Integer,N As Integer
Dim sum,f As Long
sum = 0
N = Val(InputBox("请输入一个正整数:"))
For i = 1 To N '计算N个阶乘的和
f = 1 '每次计算阶乘前把f初始化为1
For j = 1 To i '计算i的阶乘
f = f * j
Next
sum = sum + f
Next
Print sum
End Sub
【例5-13】 输入一个正整数,判断其是否为素数。
Private Sub Command1_Click()
Dim i As Integer,k As Integer,m As Integer
Dim flag As Boolean
flag = True
i = 2
m = Val(InputBox("请输入一个正整数:"))
k = Int(Sqr(m)) '求m的平方根,并转化为整数
Do While i <= k And flag = True
If m Mod i = 0 Then
flag = False
'一旦找到能够整除m的数,就把标志变量设为False
End If
i = i + 1
Loop
If flag = True Then
Print m; "是一个素数"
Else
Print m; "不是一个素数"
End If
End Sub
【例5-14】 打印出100到200之间所有的素数。
Private Sub Command1_Click()
Dim i As Integer,j As Integer,k As Integer,n As Integer
n = 1
For i = 100 To 200 '第一层循环用来控制验证那些数
k = Int(Sqr(i))
j = 2
Do While j <= k '第二层循环用来确定一个数是否为素数
If i Mod j = 0 Then Exit Do
'一旦找到能够整除m的数,则退出本层循环
j = j + 1
n = n + 1
Loop
If i > k Then
Print i,
If n Mod 4 = 0 Then Print '控制每打印4个素数换行打印
n = 1
End If
Next
End Sub
【例5-15】 随机产生一组数,求其平均值及最大值。
Private Sub Command1_Click()
Dim x(100) As Integer
n = 30
Cls ′清屏
′给数组赋值
For i = 1 To n
x(i) = Int(Rnd * Width)
Circle (x(i),300),100′在窗体上通过圆心位置表现该数的大小
Next
′求最大值
m = x(0)
For i = 1 To n
If m < x(i) Then m = x(i)
Next
′求平均值
s = 0
For i = 1 To n
s = s + x(i)
Next
e = s / n
Print
Print ″最大值为,″ & m
Print ″平均值为,″ & e
End Sub
【例5-16】 输入一串字符,统计各字母出现的次数(大小写字母不区分),并对出现的字母显示其出现的个数。
Private Sub Command1_Click()
Dim a(1 To 26) As Integer,c As String * 1
le = Len(Text1) ' 求字符串的长度
For I = 1 To le
 c = UCase(Mid(Text1,I,1)) ' 取一个字符,转换成大写
  If c >= "A" And c <= "Z" Then
j = Asc(c)-65 + 1 ' 将A~Z大写字母转换成1~26的下标
a(j) = a(j) + 1 ' 对应数组元素加1
End If
Next I
For j = 1 To 26 ' 输出字母及其出现的次数
  If a(j) > 0 Then Picture1.Print " "; Chr$(j + 64); "="; a(j);
Next j
End Sub
【例5-17】 求解我国古代数学中的“韩信点兵”问题。原问题是,三三数之,余二;五五数之,余三;七七数之,余五。问有兵多少?
For n = 1 to 105
If n Mod 3 = 2 And n Mod 5 = 3 And n Mod 7 = 5 Then
Print n
End If
Next
【例5-18】 猴子吃桃子。小猴在一天摘了若干个桃子,当天吃掉一半多一个;第二天接着吃了剩下的桃子的一半多一个;以后每天都吃尚存桃子的一半零一个,到第7天早上要吃时只剩下一个了,问小猴那天共摘下了多少个桃子?
Private Sub Form_Click()
Dim n%,i%
x = 1 ' 第7天的桃子
Print "第 7 天的桃子数为:1只"
For i = 6 To 1 Step -1
 x = (x + 1) * 2
 Print "第";i;"天的桃子数为:";x;"只"
Next i
End Sub
【例5-19】 对已知存放在数组中的6个数8,6,9,3,2,7,用选择法按递增顺序排序。
Option Base 1
Private Sub Command1_Click()
Dim iA%(1 To 10),iMin%,n%,i%,j%,t%
iA(1) = 8,iA(2) = 6,iA(3) = 9,iA(4) = 3,iA(5) = 2,iA(6) = 7
n = 6
For i = 1 To n-1 ' 进行n-1轮比较
iMin = i ' 对第i轮比较时,初始假定第i个元素最小
For j = i + 1 To n
' 在数组 i~n个元素中选最小元素的下标
If iA(j) < iA(iMin) Then iMin = j
Next j
t = iA(i)
' i~n个元素中选出的最小元素与第i个元素交换
iA(i) = iA(iMin)
iA(iMin) = t
Next i
End Sub
【例5-20】 输入10个整数,并用冒泡法从小到大排序。
Private Sub Command1_Click()
Dim a(1 To 10) As Integer
Dim i As Integer,j As Integer,t As Integer
For i = 1 To 10 '用循环输入10个整数
a(i) = Val(InputBox("请输入第" & i & "个整数:"))
Next
Print "您输入的数据是:";
For i = 1 To 10 '用循环输出10个整数
Print a(i);
Next
Print '换行输出
For i = 1 To 10 - 1 '用循环实现N趟冒泡
For j = 1 To 10 - i '用循环实现具体的一趟冒泡
'如果a(j)大于a(j + 1)则交换两个元素
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
Print "排序后的数据是:"
For i = 1 To 10
Print a(i);
Next
End Sub