作业帮 > 综合 > 作业

请教一道VB题(编写子过程好头晕)

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/11/06 08:12:48
请教一道VB题(编写子过程好头晕)
3、 过键盘输入10个整数,输出其中的最大数和平均数,并将这10个数从小到大排序输出到窗体上.要求分别编写子过程max,aven,sort 来求最大数、平均数、和排序,在窗体的单击事件过程中调用这些函数.
Option Explicit
Private Sub Form_Click()
Dim d(1 To 10) As Integer
Dim i As Integer
Dim MyMax As Integer
Dim MyAve As Single
'输入
For i = 1 To 10
d(i) = Val(InputBox("请输入第 " & CStr(i) & " 个数!"))
Next i
'调用求最大值的过程
Call Max(d,MyMax)
'输出
Print "最大值:" & MyMax
Print
'调用求平均值的过程
Call aven(d,MyAve)
'输出
Print "平均值:" & MyAve
Print
'调用排序(升序)过程
Call sort(d)
'输出
For i = 1 To 10
Print d(i); " ";
Next i
End Sub
Private Sub Max(ByRef d() As Integer,ByRef Max As Integer)
Dim i As Integer
Max = d(1)
For i = 2 To 10
If d(i) > Max Then Max = d(i)
Next i
End Sub
Private Sub aven(ByRef d() As Integer,ByRef AVE As Single)
Dim TT As Integer
Dim i As Integer
TT = 0
For i = 1 To 10
TT = TT + d(i)
Next i
AVE = TT / 10
End Sub
Private Sub sort(ByRef d() As Integer)
Dim i As Integer
Dim j As Integer
Dim T As Integer
'排序
For i = 1 To 9
For j = i + 1 To 10
If d(i) > d(j) Then
T = d(i)
d(i) = d(j)
d(j) = T
End If
Next j
Next i
End Su