VB中 求数组中数据累加和,平均数

请高手用VB语言回答。谢谢了。

直接将下面代码复制到窗体中测试即可Private Sub Form_Load()
Dim n As Integer
Dim a() As Single
Dim i As Integer
Dim sum As Single
Dim even As Single
n = CInt(Val(InputBox("请输入数组的存储的数的个数:")))
ReDim a(n) As Single
sum = 0
For i = 1 To n
a(i) = Val(InputBox("请输入第" & i & "个数(共" & n & "个)"))
sum = sum + a(i)
Next
even = sum / n
MsgBox "数组中数的和为" & sum & ",平均数为" & even
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-23
Private Sub Form_click()
Dim a(1 To 10) As Integer '定义有10个数据的整型数组
Print "原始数据是:"
For k = 1 To 10
a(k) = Int(Rnd * 101) '产生100内的随机整数
Print a(k);
Next k
For Each x In a
Sum = Sum + x
Next
Print
Print "总和是:"; Sum
Print "平均数是:"; Sum / 10
End Sub本回答被网友采纳
第2个回答  2013-12-23
Dim Num(5), Add, Average As Integer

Num(0) = 2
Num(1) = 4
Num(2) = 6
Num(3) = 8
Num(4) = 10

For i = 0 To 4
Add = Add + Num(i)
Next i

Average = Add / 5'注:不知道是否你需要的答案,但愿能帮到你
相似回答