用VB语句计算s=1+1/2!+1/3!+……+1/n!

考试急用。希望大家帮助。最好带注释。。

Private Sub Form_Click()
Form1.AutoRedraw = True
Cls '清屏
Dim n As Integer, Factorial As Double, s As Double
Factorial = 1 '赋初值,阶乘初值为1,若为0则阶乘结果永远是0
s = 0 '此处可省略,系统默认为0
n = Val(InputBox("Please enter “n”", "Tips")) '由用户输入n的值
For i = 1 To n
Factorial = Factorial * i '求阶乘
s = s + 1 / Factorial '求和
Print "n=" & i '此处3句是显示中间计算过程,可省略
Print "Factorial=" & Factorial
Print "S=" & s
Print '输出空行,将计算过程隔开,便于查看,可省略,也可加入符号,如:“Print "****************************"”
Next i
Print "最后结果:S=" & s
End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-05-05
楼上回答有误。应该有两个循环 ,也不用那么复杂
n=val(inputbox("请输入n值")) '获取n值
for i=1 to n
jc=1
for j=1 to i
jc=jc*j
next '此内层循环用来计算阶乘
s=s+1/jc '阶乘累加
next

参考资料:如果您的回答是从其他地方引用,请表明出处

第2个回答  2009-05-05
1楼 的 方法很巧!
第3个回答  2009-05-05
不好意思,刚才太匆忙,现在修改一下,应该是这样的吧:
dim i as integer,jc As Double, s As Double
n=val(inputbox("请输入n值")) '获取n值
jc = 1
s = 0
for i=1 to n
jc=jc*i '求n!
s=s+1/jc '1/n!累加
next

VB编程计算S=1+1\/2+1\/3+1\/4+...+1\/n 要求最后一项不小于0.0001
For i = 1 To n s = s + 1 \/ i Next Print s End Sub

VB编程求sum=1+1\/2+1\/3+...+1\/n
Private Sub Form_Click()Dim i As Integer Dim sum As Double Dim t As Double sum = 0 i = 1 t = 1 \/ i Do While t > 0.0001 sum = sum + t i = i + 1 t = 1 \/ i Loop Debug.Print "i="; i; "sum="; sum End Sub ...

在vb程序中,计算1+1\/2+1\/3+……+1\/n的程序
VB.NET代码:dim i,n as Longdim s as Doublen=Console.ReadLine()s=0.0for i=n to 1 step -1s=s+1.0\/inext iConsole.WriteLine("s={0:e}",s)Console.WriteLine("s={0:g}",s)

怎样写VB循环语句代码:1+1\/2+1\/3+1\/4+1\/n=?
x=inputbox("请输入N的值:","数据输入!",1)y=1 for n=1 to x y=y+1\/n next n text1=y

vb里面那几个表示循环的函数怎么用
1、运行For语句和Do语句的循环语句的功能示范.frm(注意步长为负时,如-1时For语句的运行情况讨论),掌握它们的功能;2、例2:求S=1+1\/2+1\/3+…+1\/n的循环结构程序如下:(演示:例2累加求和程序.FRM,再一次说明为什么要用第二种Do循环语句)S = 0 n = Val(Text1.Text) (因为条件表...

用VB编写函数过程,计算s=1+1\/2+1\/3+…+1\/100。运行程序时,单击窗体...
private sub Form_Click()dim i as long dim s as currency s=1 for i=2 to 100 s=s+(1\/i)next i msgbox s end sub '这个程序会弹出精确到4位小数的结果,如果需要更高精度的,可以试着把currency换成double。

各位高手求VB代码两题 1,S=1+1\/2+1\/3+1\/4...1\/n当某项<=10的-7次方...
第一题:Dim s As Double, p As Double Dim n As Long s = 0: n = 1 Do While n <= 30 p = 1 \/ n s = s + p n = n + 1 Loop Print s 第2题:设label1的AutoSize为真 Dim i As Integer, x As Integer Randomize Label1.Caption = ""For i = 1 To 20 x = Rnd...

...用两种不同的方法求1+1\/1!+1\/2!+1\/3!+…+1\/n!
Print s, i End Sub Private Sub Command2_Click()Dim s, i, k s = 1 Do i = i + 1 k = 1 \/ fc(i)If k < 10 ^ (-6) Then Exit Do s = s + k Loop Print s, i - 1 End Sub Function fc(n)Dim i fc = 1 For i = 1 To n fc = fc * i Next End ...

如何编写vb过程 s=1+1\/2+1\/3+……+1\/100
dim a,b as double b=0 a=1 for a = 1 to 100 b = b + (1\/a)next msgbox "结果为" & b 思路是,用一个变量来储存得数,用for把a(表示计算到哪里)加上去

vb编程题求1+2分之一+3分之一……+10分之一
如图所示,望采纳。。。

相似回答