用fact函数求阶乘求和的vb语言用过程法 1!+2!+3!+ +n! 调用fact函数求阶乘之和
3个回答
展开全部
Private Sub Command1_Click()
Dim a As Integer, I As Integer, Sum As Long
a = Val(Text1.Text)
For I = 1 To a
Sum = Sum + fact(I)
Next
Text2.Text = Sum
End Sub
Private Function fact(X As Integer)
Dim K As Integer, S As Long
S = 1
For K = 1 To X
S = S * K
Next
fact = S
End Function
Dim a As Integer, I As Integer, Sum As Long
a = Val(Text1.Text)
For I = 1 To a
Sum = Sum + fact(I)
Next
Text2.Text = Sum
End Sub
Private Function fact(X As Integer)
Dim K As Integer, S As Long
S = 1
For K = 1 To X
S = S * K
Next
fact = S
End Function
追问
是错的,我试了一下,1!+2!+3!+ .。。+9!应该是409113,可是用这个算出来不对
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
(“%D&N)1.scanf半引号 2严重错误少,主要功能和应给予初始为0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Option Explicit
Dim s As Integer
Private Sub Command1_Click()
Text2.Text = Str(fact(Val(Text1.Text)))
s = 0
End Sub
Private Function fact(n As Integer) As Integer
Dim i As Integer, j As Integer
j = 1
fact = 0
For i = 1 To n
j = j * i
s = s + j
Next i
fact = s
End Function
Dim s As Integer
Private Sub Command1_Click()
Text2.Text = Str(fact(Val(Text1.Text)))
s = 0
End Sub
Private Function fact(n As Integer) As Integer
Dim i As Integer, j As Integer
j = 1
fact = 0
For i = 1 To n
j = j * i
s = s + j
Next i
fact = s
End Function
追问
j = j * i
这个是错的,总是让我调试啊
追答
Option Explicit
Dim s As Long
Private Sub Command1_Click()
Text2.Text = Str(fact(Val(Text1.Text)))
s = 0
End Sub
Private Function fact(n As Integer) As Long
Dim i As Long, j As Long
j = 1
fact = 0
For i = 1 To n
j = j * i
s = s + j
Next i
fact = s
End Function
是因为溢出了。改一下类型就行了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询