Next i
Number = Val(InputBox(\输入数据\ For i = 1 To 100
If a(i) = Number Then Print i k = k + 1 End If Next i
If k = 0 Then MsgBox \不存在所查找的数\End Sub
11、编程求由一位随机整数构成的4×5二维数组A的每一列元素的和。运行界面如图所示。
Option Base 1
Private Sub Command1_Click() Dim a(4, 5) As Integer For i = 1 To 4 For j = 1 To 5
a(i, j) = Int(Rnd * 9 + 1)
Text1.Text = Text1.Text & \ Next j
Text1.Text = Text1.Text & vbCrLf Next i
For j = 1 To 5 Sum = 0
For i = 1 To 4
Sum = Sum + a(i, j) Next i
Text2.Text = Text2.Text & \ Next j End Sub
12、单击命令按钮command1时,生成包含[1,9]之间随机整数的两个5*5矩阵,分别显示在图片框Picture1和Picture2中,并求两个矩阵之和显示在图片框Picture3中。
44
Option Base 1
Dim a(5, 5) As Integer, b(5, 5) As Integer, c(5, 5) As Integer
Private Sub Command1_Click() Picture3.Cls Randomize For i = 1 To 5
For j = 1 To 5
a(i, j) = Int(Rnd * 9 + 1) b(i, j) = Int(Rnd * 9 + 1)
Picture1.Print Format(a(i, j), \ Picture2.Print Format(b(i, j), \ Next j
Picture1.Print Picture2.Print Next i
For i = 1 To 5
For j = 1 To 5
c(i, j) = a(i, j) + b(i, j)
Picture3.Print Format(c(i, j), \ Next j
Picture3.Print Next i
End Sub
13、编写程序,在窗体上打印出如图所示的杨辉三角形。要求:行数由用户指定。
Option Base 1 Dim a() As Double
Private Sub Form_Click()
n = Val(InputBox(\输入打印行数\
45
Form1.Cls ReDim a(n, n) a(1, 1) = 1 a(2, 1) = 1 a(2, 2) = 1 Print Print Print
For i = 1 To n For j = 1 To i
If j = 1 Or j = i Then a(i, j) = 1 Else
a(i, j) = a(i - 1, j - 1) + a(i - 1, j) End If
Print a(i, j); Next j Print Next i
End Sub
14、随机生成一个由两位正整数构成的5行5列矩阵显示在窗体上,并求出该矩阵中两条对角线元素之和。
Option Base 1
Private Sub Command1_Click() Dim a(5, 5) As Integer For i = 1 To 5 For j = 1 To 5
a(i, j) = Int(Rnd * 90 + 10)
If j = 1 Or i + j = 6 Then s = s + a(i, j) Print a(i, j); Next j Print Next i Print Print Print s End Sub
15、随机生成一个由两位正整数构成的6行6列矩阵显示在窗体上,并求出该矩阵中所有元素之和。
Option Base 1
Private Sub Command1_Click() Dim a(6, 6) As Integer For i = 1 To 6
46
For j = 1 To 6
a(i, j) = Int(Rnd * 90 + 10) s = s + a(i, j) Print a(i, j); Next j Print Next i Print Print Print s End Sub
16、编写一个求1+2+3+?+n的函数,单击命令按钮(Command1)时调用该函数求1+2+3+4+5的值,结果显示在一个文本框(Text1)中。
Option Base 1
Private Sub Command1_Click() Text1.Text = F(5) End Sub
Public Function F(n As Integer) As Integer For i = 1 To n F = F + i Next i
End Function
17、编写程序,判断一个数N是否是奇数。要求使用通用过程。
Private Sub Command1_Click() Dim n As Integer
n = Val(InputBox(\输入需要判断的数\ Print F(n) End Sub
Public Function F(n As Integer) As Boolean If n Mod 2 = 0 Then F = False Else
F = True
18、编写求表达式
x3?y3?z3的值的Function过程。
Public Function Fact(X As Integer, Y As Integer, Z As Integer) As Single
Fact = Sqr(Abs(X ^ 3 + Y ^ 3 + Z ^ 3))
47
End Function
19、编写一个摄氏温度与华氏温度转换的通用过程。在一个文本框(Text1)中输入摄氏温度,单击命令按钮command1时调用该通用过程在另一个文本框(Text2)中显示对应的华氏温度。摄氏温度(C)与华氏温度(F)转换的公式是:F?C?9?5?32。
Private Sub Command1_Click() Dim c As Double c = Val(Text1.Text) Text2.Text = CStr(F(c)) End Sub
Public Function F(c As Double) As Double F = c * 9 / 5 + 32 End Function
20、编写一个判断某正整数N是否是完数的通用过程。完数:一个数恰好等于它的因子之和(除自身),如:6=1+2+3,因此6是完数。
Public Function F(n As Integer) As Boolean F=False
For i = 1 To n - 1
If n Mod i = 0 Then s = s + i Next i
If s = i Then F = True End Function
21、编写一个求正整数m和正整数n的最大公约数的函数。
Public Function F(m As Integer, n As Integer) As Integer r = m Mod n While r <> 0 m = n n = r
r = m Mod n Wend F = n End Function
22、编写一个对数组元素进行排序的通用过程。
Public Sub P(X() As Integer)
Dim i As Integer, j As Integer, n as integer N=ubound(x) For i = 1 To n - 1 For j = i To n
If X(i) > X(j) Then t = X(i) X(i) = X(j) X(j) = t End If
48