利用VB制作一个“随机生成十个数字(1-100之间的整数)求极值(最大最小值)和平均值”
代码:
Dim max, min As Integer
Dim avg As Double
Private Sub Command1_Click()
Dim num(10) As Integer
Text1.Text = ""
Label4.Caption = ""
Label5.Caption = ""
Label6.Caption = ""
Dim i, j, temp, sum As Integer
sum = 0
For i = 1 To 10
num(i) = Int(Rnd * (100 + 1))
sum = sum + num(i)
Text1.Text = Text1.Text & num(i) & Space(4)
Next i
For i = 1 To 10
For j = 1 To 10 - i
If num(j) < num(j + 1) Then
temp = num(j)
num(j) = num(j + 1)
num(j + 1) = temp
End If
Next j
Next i
max = num(1)
min = num(10)
avg = sum * 1# / 10
End Sub
Private Sub Command2_Click()
If Text1.Text = "" Then
MsgBox "没有数组,无法计算!"
Else
Label4.Caption = max
Label5.Caption = min
Label6.Caption = avg
End If
End Sub
设计好的窗体:
程序运行的窗体: