我編了個程序,計算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。