VB语句问题,用递归的方法,求任意数的阶乘。
2个回答
展开全部
Private Sub Command1_Click()
n = InputBox("请输入阶数n:")
s = f(n)
Print n & "!=" & s
End Sub
Private Function f(ByVal n As Integer)
If n = 0 Or n = 1 Then
f = 1
Else
f = n * f(n - 1)
End If
End Function
n = InputBox("请输入阶数n:")
s = f(n)
Print n & "!=" & s
End Sub
Private Function f(ByVal n As Integer)
If n = 0 Or n = 1 Then
f = 1
Else
f = n * f(n - 1)
End If
End Function
追问
编写求阶乘的子过程,调用该过程计算5!+6!+7!之和。麻烦了
追答
Private Sub Command1_Click()
Dim n As Integer
n = InputBox("请输入阶数n(n>=7):")
Call sub1(n)
End Sub
Private Sub sub1(n As Integer)
Dim i As Integer, s As Integer
For i = 5 To 7
s = s + fact(i)
Next i
Print 5 & "!+" & 6 & "!+" & 7 & "!=" & s
End Sub
Private Function fact(n As Integer)
fact = 1
For i = 1 To n
fact = fact * i
Next i
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询