刚学VB求教个简单的问题,从文本框中输入数据求平均值该怎么处理

从text中读取用空格分开的数据存入数组中并求平均值该怎么操作?主要是不知道怎么生成那个数组并知道长度,我习惯C语言那种求和除以个数那种方式
最新回答
全都是假的丶

2024-10-23 07:43:22

Private Sub Command1_Click()
Dim a
a = Split(Text1.Text)
Dim s As Single
For i = LBound(a) To UBound(a)
s = s + a(i)
Next i
MsgBox s / (UBound(a) + 1)
End Sub
Private Sub Form_Load()
Text1.Text = "22 11 33"
End Sub
调试通过的,别忘了采纳
无聊也不聊

2024-10-23 07:40:50

dim Arr
Arr=split(TEXT1.TEXT," ")
s=0
for i= 0 to Ubound(Arr)-1
S=S+VAL(Arr(i)
next i

A=S/ubound(Arr)
⊿半季离殇

2024-10-23 07:40:51

处理方法如下:
Private Function AverageA(A As Variant) As Double
Dim s As Double
s = 0
For i = 0 To UBound(A)
s = s + Val(A(i))
Next
If i <> 0 Then AverageA = s / i Else AverageA = 0
End Function
Private Sub Command1_Click()
Dim A
A = Split(Trim(Text1.Text))
Text2.Text = AverageA(A)
End Sub