求一段用VB编写的求 n个数求平均值的代码

无论人工随便输入几个数值,运行程序都能得到相应的结果
2025-02-22 22:18:36
推荐回答(2个)
回答1:

Private Sub Command1_Click()
Dim myStr As String
Dim s As Double
Dim n As Long
n = 0
s = 0
Do
myStr = InputBox("请输入一个数,当输入AAA时结束输入并计算全部数的平均值。")
If UCase(myStr) <> "AAA" Then
If IsNumeric(myStr) Then
s = s + Val(myStr)
n = n + 1
Else
MsgBox "请输入数字."
End If
Else
MsgBox "以上" & Str(n) & "个数的平均值为:" & Str(s / n)
Exit Do
End If
Loop
End Sub

回答2:

a = text1.text
arr = split(a,chr(13)+chr(10))

n = 0
s = 0
for i = 0 to ubound(arr)
if not isnull(arr) then
s = s + arr(i)
n = n + 1
end if
next

平均 = s/n

---------
TEXT1 里输入数字,用回车分隔