'这题目,对于初学者是很有难度的,
'运行效果如上图,代码如下:
'有问题可以问我
Private Sub Command1_Click()
Dim arr(1 To 20) As Integer
Dim i, a, b As Integer
Dim Ave As Double
Dim sum As Integer
Dim Mx, Mn As Integer
Print "随机数数组为:"
Randomize
For i = 1 To 20
arr(i) = Int(Rnd * 101)
sum = arr(i) + sum
Print arr(i);
If i = 10 Then
Print
End If
Next i
Ave = sum / 20
Mx = Max(arr)
Mn = Min(arr)
For i = 1 To 20
If arr(i) = Mx Then
a = i
End If
If arr(i) = Mn Then
b = i
End If
Next i
Print: Print
Print "平均值为:" & Ave
Print "最大值为:" & Mx & " 对应数组下标为:" & a
Print "最小值为:" & Mn & " 对应数组下标为:" & b
End Sub
Private Function Max(List() As Integer) As Integer
Dim i As Integer
Max = List(LBound(List))
For i = LBound(List) + 1 To UBound(List)
If Max < List(i) Then
Max = List(i)
End If
Next
End Function
Private Function Min(List() As Integer) As Integer
Dim i As Integer
Min = List(LBound(List))
For i = LBound(List) + 1 To UBound(List)
If Min > List(i) Then
Min = List(i)
End If
Next
End Function