Private Sub Command1_Click()
Call pjs(Text1)
End Sub
Private Function pjs(txt As TextBox)
Dim sum() As String, a As Single
sum = Split(txt, ",")
For i = 0 To UBound(sum)
a = a + sum(i)
Next i
MsgBox "平均数为:" & a / i
End Function
用下面的函数,返回值就是数组平均值
函数调用方法ArrAvrg(aa())
Private Function ArrAvrg(a() As Double) As Double
Dim i As Integer, n As Integer
For i = LBound(a) To UBound(a)
ArrAvrg = ArrAvrg + a(i)
Next i
n = UBound(a) - LBound(a) + 1 <> 0
If n <> 0 Then ArrAvrg = ArrAvrg / n
End Function
太简单了,首先要确定组数,再就是循环累加,然后就是求值了。
for i=1 to val(text1.text)
s=s+val(inputbox())
next i
val=s/val(text1.text)
msgbox(val)
我好长时间没有用vb了,不过基本的思想都有了!!
Average()