VB程序设计题:编写程序,计算N!的值(N为键盘输入的一个整数)
3个回答
展开全部
Private Sub Command1_Click()
Dim i As Integer, n As Integer, s As Double
s = 1
n = Val(Text1.Text)
If n <= 1 Then
Text2.Text = "1"
Exit Sub
Else
For i = 1 To n
s = s * i
Next i
Text2.Text = Format(s, "0")
End If
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Command1.Caption = "计算"
End Sub
追问
嗯,这就是问题答案吗
可是这是要写阶乘呀
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
‘调用示例
Private Sub Form_Load()
MsgBox fun_JC(3)
End Sub
'阶乘函数
Private Function fun_JC(ByVal N As Integer) As Long
Dim i As Integer
Dim l As Long
l = 1
If N <= 1 Then
fun_JC = 1
Exit Function
End If
For i = N To 2 Step -1
l = l * i
Next
fun_JC = l
End Function
Private Sub Form_Load()
MsgBox fun_JC(3)
End Sub
'阶乘函数
Private Function fun_JC(ByVal N As Integer) As Long
Dim i As Integer
Dim l As Long
l = 1
If N <= 1 Then
fun_JC = 1
Exit Function
End If
For i = N To 2 Step -1
l = l * i
Next
fun_JC = l
End Function
追问
谢谢,能给我解释下吗
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询