热血最强.:求VB难题

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/02 23:55:50
求sum=1+1/2+1/3+…+1/n,直到第n项(1/n)的值小于0.0001时为止。

谢谢~

Dim i As Integer
Dim sum As Double
i = 1
Do While 1 / i >= 0.0001
sum = sum + 1 / i
i = i + 1
Loop
MsgBox sum

输出结果是:9.7806036

用个循环判断语句不就行了吗?

dim a as long
a=2
for i=2 to 100000
a=a/i+1
if a <0.001 then
exit for
end if
next

试试吧,我机器没vb,运行不了

dim n as integer
dim sum as single
n=1
do
sum=sum+1/n
n=n+1
loop while 1/n<0.0001

欢迎加入24885035群,请说明意图
VB,VB.NET交流
代码成就天地人生,程序造就你我辉煌
让我们在程序的路上一起走得更远!

1. 设计界面
添加控件:Label1,Command1,Command2

2. 程序如下:

Private Sub Form_Load()
Label1.Caption = ""
Label1.BorderStyle = 1
Command1.Caption = "计算"
Command2.Caption = "退出"
End Sub

Private Sub Command1_Click()
Sum = 0
n = 1
Do
Sum = Sum + 1 / n
n = n + 1
Loop Until 1 / n < 0.0001
Label1.Caption = Sum
End Sub

Private Sub Command2_Click()
End
End Sub