用VB编程计算 s=1-1/2+1/3-1/4+.....+(-1)的n-1次方/n

来源:百度知道 编辑:UC知道 时间:2024/05/29 06:25:25
用VB实现
s精确到0.0001

Dim s, i, n As Integer
s = 0
n = cint(text1)
For i = 1 To n
s = s + (-1) ^ (i - 1) / i
Next
Print s

Private Sub Command1_Click()
Dim fuhao As Boolean
Dim he As Double, i As Double
Do
n = Val(InputBox("请输入截至项的分母(大于0):"))
Loop Until n > 0
fuhao = True
For i = 1 To n
If fuhao Then
he = he + 1 / i
Else
he = he - 1 / i
End If
fuhao = Not fuhao
Next i
Print n, he
End Sub

就一个按钮