求VB 阶乘函数 fact(n)

求VB 阶乘函数 fact(n),第1张

Function fact(ByVal n As Integer) As Long

Dim i As Integer

Dim s As Long

s = 1

For i = 1 To n

s = s i

Next

fact = s

End Function

递归法,下面这个是个编写出来的阶乘函数,直接调用就可以了

Private Function jiecheng(n As Integer) As Integer

If n = 1 Then

jiecheng = 1

Else

jiecheng = n jiecheng(n - 1)

End If

End Function

我在习惯上做一个块,供事件方法调用。

它灵活在,你可以传递参数,可以是其它数的阶乘!!

Private Sub Command1_Click()

MsgBox "100!= " & Total(100) ’从此入口,调用下面的函数,并返回值

End Sub

Function Total (num as Double)

Dim i As Iinteger

num = 1

For i = 1 To 100

num = num i

Next

return Total

End Function

========================================

您的问题==我的课题 奉献知识==辉煌生命

黑龙江省 张志晨

========================================

Public Function Fac(ByVal n As integer) As Long

Dim i As Integer

For i=1 to n

Fac=Faci

Next i

End Function

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/12180900.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存