求教一道简单的VB题,直到最后一项的绝对值小于10-5为止。 1-1⼀(1*2)+1⼀(2*3)-

2024-11-16 18:42:13
推荐回答(3个)
回答1:

Private Sub Command1_Click()
Dim a As Double, i As Long, s As Single
f = -1
i = 1
Do
a = 1 / (i * (i + 1))
s = 1 + f * a
i = i + 1
f = -f
Loop Until Abs(a) < 10 ^ (-5)
Print s
End Sub

回答2:

改动这两句,特别是要定义为Variant即可
Dim a As Variant, s As Variant, i As Variant
s = s + f * a
程序:
Private Sub Command1_Click()
Dim a As Variant, s As Variant, i As Variant
f = -1
i = 1
s = 1
Do
a = 1 / (i * (i + 1))
s = s + f * a
i = i + 1
f = -f
Loop Until Abs(a) < 10 ^ (-5)
Print S
End Sub

回答3:

Public Function MyCos(ByVal x As Double) As Double
Dim i As Integer, sum As Double, a As Double
i = 2
sum = 1
a = 1
Do
a = -a * x * x / i / (i - 1)
sum = sum + a
i = i + 2
Loop Until Abs(a) < 10 ^ -7
MyCos = sum
End Function