vb求阶乘的问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 00:49:05
求2!+4!+…+n!的和(n<=10)怎么运行都不对,但没有提示错误.....
Dim i%, n&, ee!, j%, x%
x = Val(InputBox("input 2~10"))
Do While x > 10 Or x Mod 2 <> 0
MsgBox("please input 2~10")

Loop
ee = 0

For i = 2 To 10 Step 2
n = 1
For j = 1 To i
n = n * j

Next
ee = ee + n

Next
MsgBox(ee)
End Sub
msgbox弹出的都是3669866....怎么回师啊

x = Val(InputBox("input 2~10"))
这句放到do while之下好点.如果输错的话你的代码会无限弹msgbox
for ……next 你的next后面跟上循环变量 next i. next j.

刚才看错了

我帮你改了:
Private Sub Form_Click()
Dim i%, n&, ee!, j%, x%
mylabel:
x = Val(InputBox("input 2~10"))

If x > 10 Or x Mod 2 <> 0 Then '用if就可以了,不用while
MsgBox ("请输入2~的偶数,点确定后重新输入")
GoTo mylabel '输入不是偶数,就重新输入
End If

e = 0

For i = 2 To x Step 2 '从2到你输入的x,而不是10,如果是10,不论输入几,都是到10的阶乘和
n = 1
For j = 1 To i
n = n * j

Next
ee = ee + n

Next

MsgBox (ee)

End Sub