1、VB 用Rnd函数随机产生30个学生的成绩,用数组存放

来源:百度知道 编辑:UC知道 时间:2024/09/23 10:27:00
1)各分数段人数,0~59、60~69、70~79、80~89、90~100
(2)最高分及学号、最低分及学号和平均分。

Dim d(1 To 10) As Integer
Option Explicit
Dim i%, k%, l%, m%

Private Sub Command1_Click()
Label1.Caption = "产生的平均数为:"
For i = 1 To 10
Randomize
d(i) = Fix(Rnd * 71 + 30)
Label1.Caption = Label1.Caption & d(i) & " "

'取最大、最小值
If i > 1 Then
k = maxi(k, d(i))
l = mini(l, d(i))
Else
k = d(i)
l = d(i)

End If

'取平均值
m = m + d(i) / 10

Next i

Label2.Caption = "平均值为:" & m & " 最大值为:" & k & " 最小值为:" & l

Label1.Visible = True
Label2.Visible = True

m = 0

End Sub

Public Function maxi(x As Integer, y As Integer) As Integer
If x > y Then
maxi = x
Else
maxi = y
End If

End Function
Public Function mini(x1 As Integer, y1 As Integer) As Integer
If x1 < y1 Then
mini = x1
Else
mini = y1
End