编写vb程序,求s=a!+b!+c!,阶乘的计算用sub过程方法实现,谢谢

2025-01-04 22:19:07
推荐回答(3个)
回答1:

Public Sub t()
a=2:b=3:c=4
阶乘 p, a :S=p
阶乘 p, b :S=S+p
阶乘 p, c :S=S+p
Print S
End Sub

Public Sub 阶乘(ByRef P, ByVal N)
'注意要返回值的 用 ByRef
P= 1
For i = 1 To N
P= P * i
Next
End Sub

回答2:

sub是没有返回值的。在这里我用function举个例吧。。如果用sub的话就只能用控件显示或许用msgbox
function jiecheng(byval n as single)
s=1
for i=1 to n
s=s*n
next
jiecheng=s
end function
sub sum(a,b,c)
msgbox jiecheng (a)+jiecheng(b)+jiecheng(c)
end sub
或者传引用也行
sub jiecheng(byref n as single)byref可以省略
s=1
for i=1 to n
s=s*n
next
n=s
end sub

回答3:

多次调用函数问题
Private Sub Command1_Click()
Dim a As Long, b As Long, c As Long
a = Val(InputBox("输入a", "提示"))
b = Val(InputBox("输入b", "提示"))
c = Val(InputBox("输入c", "提示"))

Dim r as Long
r=Multiply(a)+Multiply(b)+Multiply(c)
Print r
End Sub
Private Function Multiply(n As Long) As Long
Dim x As Long
Multiply = 1
For x = 1 To n
Multiply = Multiply * x
Next
End Function