我编了个程序,计算sum=1!+2!+3!+……+n! ,n 的值由text1输入,窗体的设置如下,我的程序源代码是这样的Option ExplicitPrivate Sub Command1_Click() Dim Sum As Integer Dim N As Integer Dim i As Integer N = Val(Text1.Text) If N <= 0 And N > 20 Then Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) End If For i = N To 1 Step -1 Sum = Sum + Fact(i) Next i Text2 = SumEnd SubPrivate Function Fact(N As Integer) As Double Fact = 1 Do While N > 0 Fact = Fact * N N = N - 1 LoopEnd Function但是我的运行结果是sum 的值仅仅是n!而不是sum=1!+2!+3!+……+n!请各位兄弟帮我看看问题在那里?万分感谢!
热心网友
请改用以下经修改的代码,工程文件上传。Option Explicit Dim Sum As Double Dim N As Integer Dim i As Integer Dim j As IntegerPrivate Sub Command1_Click() For j = 1 To Val(Text3) N = Val(Text1。Text) If N 20 Then Text1。SelStart = 0 Text1。SelLength = Len(Text1。Text) End If For i = N To 1 Step -1 Sum = Sum + fact(i) Next i Text2 = Sum Text1 = Val(Text1) + 1 Next j Command1。Enabled = FalseEnd SubPrivate Function fact(N As Integer) As Double fact = 1 Do While N 0 fact = fact * N N = N - 1 LoopEnd FunctionPrivate Sub Form_Load() Text1 = 1: Text2 = "": Text3 = ""End Sub。