求vb题代码:求任意个数的阶乘累计和。s=1!+2!+3!+...+n!,n由用户输入。
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
Private Sub CommandButton1_Click()
Dim i, s, k
Dim n
aaa: n = InputBox("请输入大于等于1的数字")
If Val(n) >= 1 Then
If Len(n) = 0 Then End
For i = 1 To n
s = s + i * i
Next
MsgBox "从1到" & n & "阶乘累计和为:" & s
Else
k = MsgBox("你输入数据不符合标准,请重输入:", vbOKCancel, vbInformation, "提示")
If k = vbOK Then
GoTo aaa
Else
End
End If
End If
End Sub