4个回答
展开全部
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
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
展开全部
楼上回答有误。应该有两个循环 ,也不用那么复杂
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
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
参考资料: 如果您的回答是从其他地方引用,请表明出处
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼 的 方法很巧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不好意思,刚才太匆忙,现在修改一下,应该是这样的吧:
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询