标题:[讨论]帮我改改调用Sub 过程
只看楼主
xz3501
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2007-5-12
 问题点数:0 回复次数:3 
[讨论]帮我改改调用Sub 过程

1. 编写计算1!+2!+3!+……n!的Sub过程,在窗体的Click事件中调用该过程,输出计算结果。
我写的代码:
Sub factS(ByVal n As Integer, fac)
fac = 1
For i = 1 To n
fac = fac * i
nexti
End Sub


Private Sub Form_Click()
Dim a As Integer, b As Integer
a = Val(InputBox("输入项数"))
Call factS(a, fac)
??????????

Print fac;
End Sub


[此贴子已经被作者于2007-6-4 16:11:52编辑过]

搜索更多相关主题的帖子: Sub 改改 
2007-06-02 10:28
slore
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1108
专家分:0
注 册:2005-7-1
得分:0 
factS是将n的阶乘保存在fac里面。

的按钮事件需要个循环累加……这个fac
fac要是模块级变量

快上课了……
2007-06-02 13:25
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
得分:0 
给你写个循环的,至于怎么用SUB的话自己修改一下就可以了。
Private Sub Form_Click()
Dim I As Long, J As Long, SUMA As Double, SUMB As Double
J = Val(InputBox("输入项数"))
If J > 170 Then MsgBox "错误:" & vbCrLf & " 你所输入的数据太大,会导致程序溢出!": Exit Sub
SUMA = 0: SUMB = 1#
For I = 1 To J
SUMB = SUMB * I
SUMA = SUMA + SUMB
Next
Print SUMA;
End Sub

VB QQ群:47715789
2007-06-02 15:44
xz3501
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2007-5-12
得分:0 

我的代码在下面,哪里有问题啊?
Sub factS(ByVal n As Integer, fac)
fac = 1
For i = 1 To n
fac = fac * i
Next i
End Sub


Private Sub Form_Click()
Dim a As Integer, b As Integer, sum As Double
a = Val(InputBox("输入项数"))
b = 0
For i = 1 To a
b = b + 1
Call factS(a, fac)
sum = sum + fac
Next i
Print fac;
End Sub


[此贴子已经被作者于2007-6-4 18:58:10编辑过]


2007-06-04 16:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-144256-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.887063 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved