VB方面的:利用随机函数产生20个50--100范围内的随机数,显示它们的最大值,最小值,和平均值

希望大家赶快回复我哦!谢谢
2024-11-27 00:26:22
推荐回答(3个)
回答1:

Private Sub Form_click()
Dim n(1 To 20) As Integer
Dim i, j, ave, temp
Randomize
For i = 1 To 20
n(i) = Int((51 * Rnd) + 50)
Print n(i)
Next i

For j = 1 To 19
For i = j + 1 To 20
If n(i) >= n(j) Then
temp = n(i)
n(i) = n(j)
n(j) = temp
End If
Next
Next
ave = 0
For i = 1 To 20
ave = ave + n(i)
Next
ave = ave / 20
Print "最大值为:"; n(1)
Print "最小值为:"; n(20)
Print "平均值为:"; ave

End Sub

回答2:

Private Sub Command1_Click()
Dim a%, b%, c%, max%, min%, ave%
a = Int((50 - 20 + 1) * Rnd + 20)
b = Int((50 - 20 + 1) * Rnd + 20)
c = Int((50 - 20 + 1) * Rnd + 20)
If a > b And a > c Then
max = a
ElseIf b > c And b > c Then
max = b
Else
max = c
End If

If a < b And a < c Then
min = a
ElseIf b < c And b < c Then
min = b
Else
min = c
End If
ave = (a + b + c) / 3
Print "这三个数分别是"; a & " ," & b & ", " & c
Print "最大值是 " &max & ",最小值是 " & min & ",平均值是 "; ave

End Sub

回答3:

Dim a(20) As Double
Dim max As Double
Dim min As Double
Dim ave As Double
Dim total As Double

Private Sub Command1_Click()
rndNumbers
MaxAndMin
aveNumbers
End Sub

Private Sub Form_Load()
lstAll.Text = ""
'lstAll.Enabled = False

txtMax.Text = ""
txtMax.Enabled = False

txtMin.Text = ""
txtMin.Enabled = False

txtAverage.Text = ""
txtAverage.Enabled = False
End Sub

Private Sub rndNumbers()
Randomize
For i = 1 To 20
a(i) = Int(Rnd * 50 + 50)
'a(i) = Rnd * 50 + 50
lstAll.AddItem a(i)
Next i
End Sub

Private Sub MaxAndMin()
max = a(1)
min = a(1)
For i = 2 To 20
If max >= a(i) Then
max = a(i)
End If
If min <= a(i) Then
man = a(i)
End If
Next i
txtMax = max
txtMin = min
End Sub

Private Sub aveNumbers()
total = 0
For i = 1 To 20
total = total + a(i)
Next i
ave = total / 20
txtAverage.Text = ave
End Sub
==========
lstAll: ListBox txt~:TextBox