VB利用随机函数产生100个100~255(包括100和255)范围内的随机数,显示它们的最大值、最小值和平均值。
2个回答
展开全部
Dim i As Integer, a(100) As Integer, sum As Long
Private Sub Command1_Click()
Me.Cls
Print "100个随机数:"
Randomize
For i = 1 To 100
a(i) = Int((Rnd() * (255 - 100 + 1)) + 100)
sum = sum + a(i)
Print a(i);
If i Mod 10 = 0 Then Print
Next i
Print
End Sub
Private Sub Command2_Click()
max = a(1): min = a(1): maxi = 1: mini = 1
For i = 2 To 100
If a(i) > max Then max = a(i): maxi = i
If a(i) < min Then min = a(i): mini = i
Next i
Print "第" & maxi & "个元素最大=" & max & " " & "第" & mini & "个元素最小=" & min
Print
Print "平均值:" & sum / 100
End Sub
Private Sub Command1_Click()
Me.Cls
Print "100个随机数:"
Randomize
For i = 1 To 100
a(i) = Int((Rnd() * (255 - 100 + 1)) + 100)
sum = sum + a(i)
Print a(i);
If i Mod 10 = 0 Then Print
Next i
End Sub
Private Sub Command2_Click()
max = a(1): min = a(1): maxi = 1: mini = 1
For i = 2 To 100
If a(i) > max Then max = a(i): maxi = i
If a(i) < min Then min = a(i): mini = i
Next i
Print "第" & maxi & "个元素最大=" & max & " " & "第" & mini & "个元素最小=" & min
Print "平均值:" & sum / 100
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询