1.添加一个窗体 Form1,在窗体中放一个按钮 Command1
2.添加一个类模块:Class1
3.----------将下面代码复制到窗体代码中:
Private Sub Form_Load()
Command1.Caption = "计算得分"
End Sub
Private Sub Command1_Click()
Dim nFen As New Class1, S As Single
S = nFen.DeFen(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
MsgBox "去掉一个最高分,去掉一个最低分,平均得分:" & S, vbInformation
End Sub
4.-----------将以下代码复制到类模块:
Public Property Get DeFen(ParamArray Fen()) As Single
Dim S1 As Long, S2 As Long, I As Long, Min As Single, Max As Single
S1 = LBound(Fen): S2 = UBound(Fen)
'查找最低和最高分
Min = Fen(S1): Max = Fen(S1)
For I = S1 To S2
If Fen(I) < Min Then Min = Fen(I)
If Fen(I) > Max Then Max = Fen(I)
Next
'计算总分
For I = S1 To S2
S = S + Fen(I)
Next
DeFen = (S - Min - Max) / (S2 - S1 + 1)
End Property