VB,计算S=1!+2!+3!+4!+5!的程序.

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/23 08:07:02
VB,计算S=1!+2!+3!+4!+5!的程序.VB,计算S=1!+2!+3!+4!+5!的程序.VB,计算S=1!+2!+3!+4!+5!的程序.PrivateSubCommand1_Click(

VB,计算S=1!+2!+3!+4!+5!的程序.
VB,计算S=1!+2!+3!+4!+5!的程序.

VB,计算S=1!+2!+3!+4!+5!的程序.
Private Sub Command1_Click()
Dim i As Double
Dim n As Double
Dim sum As Double
sum = 0
If Not IsNumeric(Text1.Text) Then '判断是否为数字,不是数字就弹出对话框并对出过程
Call msg '调用已经写好的过程
Exit Sub
End If
n = Text1.Text

If n < 1 Then '判断是否小于1 ,如果小于1就调用已经定义好的过程弹出对话框
Call msg
Exit Sub

End If
For n = 1 To n '求1!到n!阶乘循环n次

sum = sum + func(n) '调用求第n数的的阶乘的函数,计算前1!+n!的和
Next n
MsgBox "1!+2!+3!+.+n!为" & sum, vbInformation, "结果"
End Sub
'求第n个数的阶乘函数
Function func(n As Double) As Double
Dim i As Double
Dim jc As Double
jc = 1
For i = 1 To n
jc = jc * i
Next i
func = jc
End Function
'弹出对话框
Sub msg()
MsgBox "你输入数据不正确", vbInformation, "提示"
End Sub
把文本框中填写5就可以得到答案