Dim jc As Double
jc = 1
For i = 1 To n
jc = jc * i
Next
jiecheng = jc
End Function
2、具体用法:首先打开VB6.0,新建一个标准exe工程;
3、在设计界面上添加一个Command控件,Command1的Caption属性设置为"N的阶乘"。
4、双击Command1控件进入代码编辑界面,编辑代码如下:
Public Function jiecheng(ByVal n As Integer) As DoubleDim jc As Double
jc = 1
For i = 1 To n
jc = jc * i
Next
jiecheng = jc
End Function
Private Sub Command1_Click()
Dim sum As Double, n As Integer
n = InputBox("计算的N的阶乘,请输入数字N")
sum = jiecheng(n)
MsgBox (sum)
End Sub
5、运行新建的工程,单击按钮,输入要计算的N的阶乘的数字N,得出阶乘结果。
VB求阶乘需要Function 过程来实现。
Function 语句,声明 Function 过程的名称,参数以及构成其主体的代码。
以下是求输入数的阶乘代码:
Option ExplicitDim Sum As Double
Dim N As Integer
Dim i As Integer
Private Function fact(N As Integer) As Double
fact = 1
Do While N > 0
fact = fact * N
N = N - 1
Loop
End Function
Private Sub Command1_Click()
N = Val(Text1.Text)
Sum = fact(N)
Text2 = Sum
End Sub
Private Sub Form_Load()
Text1 = "": Text2 = ""
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)