急急急!!!!!VB自定义一个计算阶乘的函数过程fac(n),调用该函数过程计算s=1!+2!+3!+4!+5!+…+n!
4个回答
展开全部
看了上面几位的代码 :应该都写错了
楼主的问题是:s等于 1到N之间每个整数的阶乘相加,而不是N的阶乘
我没理解错吧?
稍等 完毕:
在模块中输入代码:
Public Function fac(n As Integer) As Long '在模块中用public(公用的)定义函数过程
Dim i As Integer, j As Integer, s As Long
For i = 1 To n
s = 1
For j = 1 To i
s = s * j
Next j
fac = fac + s
Next i
End Function
在窗体中输入代码:
Private Sub Form_Click()
Dim s As Long
Dim n As Integer
n = InputBox("请输入一个整数")
s = fac(n)
Print s
End Sub
楼主的问题是:s等于 1到N之间每个整数的阶乘相加,而不是N的阶乘
我没理解错吧?
稍等 完毕:
在模块中输入代码:
Public Function fac(n As Integer) As Long '在模块中用public(公用的)定义函数过程
Dim i As Integer, j As Integer, s As Long
For i = 1 To n
s = 1
For j = 1 To i
s = s * j
Next j
fac = fac + s
Next i
End Function
在窗体中输入代码:
Private Sub Form_Click()
Dim s As Long
Dim n As Integer
n = InputBox("请输入一个整数")
s = fac(n)
Print s
End Sub
追问
救星,界面上还有有两个text,一个用来输入n,一个用来输入s,n是个数,s是总和,你能不能把它俩也定义下来,让我在界面输入n后可以点击计算命令按钮得到s,我完全迷茫啊……
追答
汗,主过程都这么明白了 还不会改啊 你等下 我改下
函数过程不变 然后再窗体上的Text1输入N 的值 ,单击按钮 计算 ,结果会出现在text2中
在窗体中输入代码:
Private Sub Command1_Click() '按钮单击事件
Dim s As Long
Dim n As Integer
n =val(text1.text)
s = fac(n)
text2.text = s
End Sub
展开全部
双击模块,选插入菜单里面的插入过程,选插入函数,命名为fac
代码如下:
Public Function fac(n As Integer)
Dim i As Integer, s As Integer
For i = 1 To n
s = s + i
Next
fac = s
End Function
代码如下:
Public Function fac(n As Integer)
Dim i As Integer, s As Integer
For i = 1 To n
s = s + i
Next
fac = s
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click() 过程方法
Dim i As Integer
For i = 1 To 10
Sum = Sum + jc(i)
Next
MsgBox Sum
End Sub
Function jc(a As Integer) 函数方法
jc = 1
For i = 1 To a
jc = jc * i
Next
End Function
请参考
Dim i As Integer
For i = 1 To 10
Sum = Sum + jc(i)
Next
MsgBox Sum
End Sub
Function jc(a As Integer) 函数方法
jc = 1
For i = 1 To a
jc = jc * i
Next
End Function
请参考
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Function fac(ByVal n As Integer) As Single
Dim I As Integer
Dim J As Single
J = 1
For I = 1 To n
J = J * I
Next I
fac = J
End Function
Dim I As Integer
Dim J As Single
J = 1
For I = 1 To n
J = J * I
Next I
fac = J
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询