用vb做 利用随机函数产生20 个50~100范围内的随机整数,显示它们的最大值、最小值和平均值

2024-11-22 06:28:26
推荐回答(2个)
回答1:

Private Sub Form_Click()
Dim i, num(1 To 20) As Integer, smax As Integer, smin As Integer, savg As Single

savg = 0
Print "50-100范围内的20个随机数如下:"
For i = 1 To 20
num(i) = 50 + Int(51 * Rnd) '产生[50,100]区间的20个随机数
If i = 1 Then smax = num(i): smin = num(i)
If smax < num(i) Then smax = num(i) '求最大值
If smin > num(i) Then smin = num(i) '求最小值
savg = savg + num(i) '求平均值,最后结果要除以数目
Print num(i);
If i Mod 10 = 0 Then Print
Next i

savg = savg / 20
Print
Print "最大值"; smax, "最小值"; smin, "平均值"; savg
End Sub

回答2:

Dim i, num(1 To 20) As Integer, smax As Integer, smin As Integer, savg As Single

savg = 0
MsgBox( "50-100范围内的20个随机数如下:")
For i = 1 To 20
num(i) = 50 + Int(51 * Rnd) '产生[50,100]区间的20个随机数
If i = 1 Then smax = num(i): smin = num(i)
If smax < num(i) Then smax = num(i) '求最大值
If smin > num(i) Then smin = num(i) '求最小值
savg = savg + num(i) '求平均值,最后结果要除以数目
Msgbox( num(i));
If i Mod 10 = 0 Then Print
Next i
savg = savg / 20
Print
Msgbox ("最大值"; smax, "最小值"; smin, "平均值"; savg)