vb函数调用

来源:百度知道 编辑:UC知道 时间:2024/05/15 04:58:48
Option Base 1
Function Fun(a() As Single) As Single
自己编写

End Function

Private Sub Command1_Click()
Dim arr
Dim arr2(40) As Single
arr = Array(12.5, 65.4, 56, 73, 46.23, 58.25, 83.49, 96, 27.3, 36.78, _
29.26, 89.4, 34.7, 45.4, 56.9, 67.4, 78, 98, 87.5, 76.8, _
34.6, 55.8, 64.7, 43.7, 99.4, 84.7, 66.2, 23.9, 55.5, 44.1, _
33.2, 43.5, 41.2, 28.3, 78.6, 98.3, 56.1, 73.6, 49.3, 48.3)
For i = 1 To UBound(arr)
arr2(i) = arr(i)
Next i
r = Fun(arr2())
Text1.Text = Str(r)
SaveData
End Sub

Sub SaveData()
Open App.Path + "\out3.txt" For Output As #1
Print #1, Text1.Text
Close 1
End Sub

计算数组arr中每个元素与其下标相除所得的和

Function Fun(a() As Single) As Single
Dim i As Integer
For i = 1 To UBound(a)
Fun = Fun + a(i) / i
Next
End Function
'调试通过

for i = a.lbound to a.ubound
sum = sum + a(i)/a.lbound
next
fun=sum
楼下的 数组的下标貌似应该为0?

Close 1改成Close #1,其他没看出什么毛病。