来帮忙怎么用for each……next 实现100以内各奇数阶乘之和?
如题!新手上路,还请在家多多指导哟,呵呵,谢谢!
Option Explicit Private Type ABC MyCollection() As Long End Type Private Num As ABC Private Sub Form_Initialize() ReDim Num.MyCollection(0) End Sub Private Sub Form_Load() Dim MyObject As Variant Dim i As Long, j As Long, k As Long, Total As Long For i = 0 To 100 If i Mod 2 = 1 Then ReDim Preserve Num.MyCollection(j) Num.MyCollection(j) = i j = j + 1 End If Next i Total = 0: i = 0: j = 0: k = 0 For Each MyObject In Num.MyCollection ' 對集合物件裏的成員作一次迴圈執行。 Do While i < MyObject + 1 For k = i To MyObject Total = Total + i Next k i = i + 1 If i = 100 Then Exit Do Loop Text1.Text = CLng(Text1.Text) + Total Next MyObject MsgBox Text1.Text End Sub
Option Explicit Private Sub Form_Load() Dim i As Long, j As Long, k As Long, Total As Long Dim ii As Integer Total = 0: i = 0: j = 0: k = 0 For ii = 0 To 100 If ii Mod 2 = 1 Then j = ii Do While i < j + 1 For k = i To j Total = Total + i Next k i = i + 1 Loop Text1.Text = CLng(Text1.Text) + Total End If Next ii MsgBox Text1.Text End Sub
Option Explicit Private Type ABC MyCollection() As Long End Type Private Num As ABC Private Sub Form_Initialize() ReDim Num.MyCollection(0) End Sub Private Sub Form_Load() Dim MyObject As Variant, Limit As Integer Dim i As Double, j As Double, k As Double, Total As Double Total = 0: i = 0: j = 0: k = 0 Limit = Val(InputBox("Limit Number is ?")) For i = 0 To Limit If i Mod 2 = 1 Then ReDim Preserve Num.MyCollection(j) Num.MyCollection(j) = i j = j + 1 End If Next i For Each MyObject In Num.MyCollection ' 對集合物件裏的成員作一次迴圈執行。 i = MyObject : k = 1 For j = i To 1 Step -1 k = k * j Next j Total = Total + k : Text1.Text = Total Next MyObject Text1.Text = "Sum : " & Total '可有可無 End Sub