VB程序设计习题集(附答案)

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

联系客服:779662525#qq.com(#替换为@)