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
1、For语句实现
Private Sub Command1_Click()
Dim s As Long, n As Integer, i As Integer
n = Val(Text1.Text)
s = 1
For i = 1 To n
s = s * i
Next i
Label4.Caption = Str(s)
End Sub
2、Do While语句实现
Private Sub Command1_Click()
Dim s As Long, n As Integer, i As Integer
n = Val(Text1.Text)
s = 1
i = 1
Do While i <= n
s = s * i
i = i + 1
Loop
Label4.Caption = Str(s)
End Sub
扩展资料:
1~10的阶乘的结果如下:
1!=1
2!=2*1=2
3!=3*2*1=6
4!=4*3*2*1=24
5!=5*4*3*2*1=120
6!=6*5*4*3*2*1=720
7!=7*6*5*4*3*2*1=5040
8!=8*7*6*5*4*3*2*1=40320
9!=9*8*7*6*5*4*3*2*1=362880
10!=10*9*8*7*6*5*4*3*2*1=3628800
来不及写,教你怎么做吧先把文本框的读取转存成整数
对这个整数用if判断
不满足条件用msgbox提示
满足条件的话,调用函数,参数就是那个整数,返回值就是结果。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)