VB找出与平均数最接近的整数

来源:百度知道 编辑:UC知道 时间:2024/06/22 05:15:38

用循环语句完成,假如有n个数,平均数为m
for i=1 to n
dim x(n-1)
x(i)=abs(i-m)
max=x(1)
if x(i)>max then
x(i)=max
s=i
end if
next

一个简单的例子,你可以从其中看到使用方法:
Private Sub Command1_Click()
Dim a(1 To 10) As Double
a(1) = 2.1
a(2) = 3.2
a(3) = 4.4
a(4) = 6.1
a(5) = 7.02
a(6) = 8.9
a(7) = 6.6
a(8) = 16.33
a(9) = 11
a(10) = 10
Dim aver As Double
Dim i As Integer
aver = 0
i = 1
For i = 1 To 10 Step 1
aver = aver + a(i)
Next
aver = aver / 10

Dim result As Integer

result = Round(aver)

MsgBox result
End Sub