2022-01-28|閱讀時間 ‧ 約 2 分鐘

程式碼基礎練功.3

計算 1+2+3+... ,當加到多少時,剛好大於 1000 計算 N+(N-1)+(N-2)+... ,當加到多少時,剛好大於 1000
Sub TestSumSoluation() Dim iiKeyNum As Integer Dim iiSoulation As Integer Dim iiThreshold As Integer Dim iiStep As Integer Dim BottomLimit As Integer Dim TopLimit As Integer
'Input iiStep = 2 BottomLimit = 1 TopLimit = 100 '********************************************* iiSoulation = 0 iiThreshold = 1000 For iiKeyNum = BottomLimit To TopLimit Step iiStep iiSoulation = iiSoulation + iiKeyNum If iiSoulation iiThreshold Then Exit For Next 'iiKeyNum = 1 To 1000 Debug.Print iiKeyNum '********************************************** iiSoulation = 0 iiThreshold = 1000 For iiKeyNum = TopLimit To BottomLimit Step -iiStep iiSoulation = iiSoulation + iiKeyNum If iiSoulation iiThreshold Then Exit For Next 'iiKeyNum = 1 To 1000 Debug.Print iiKeyNum End Sub
分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.