EX:N為10時,則印出1.3.5.7.9
要用while-wend或Do-loop寫喔,不能用for-next
期末考要考這題,請大家幫幫忙>_< 盡量簡單,讓我能快速了解
2007-01-06 13:01:49 · 1 個解答 · 發問者 小恩 4 in 電腦與網際網路 ➔ 程式設計
Private Sub Command1_Click()
Dim I As Long, N As Long
Do
N = Int(Val(InputBox("請輸入N (0
I = 1
Do
If IsPrime(I) Then Print I;
I = I + 1
Loop Until I > N
End Sub
Function IsPrime(N As Long) As Boolean '判斷質數
Select Case N
Case 1 To 3: IsPrime = True
Case Else
If N Mod 2 Then
Dim I As Long, E As Long
I = 3: E = Int(Sqr(N))
Do
If N Mod I = 0 Then Exit Function
I = I + 2
Loop Until I > E
IsPrime = True
End If
End Select
End Function
2007-01-08 18:13:07 補充:
Loop Until N > 1 And N <= 2147483647
請改成
Loop Until N > 0 And N <= 2147483647
2007-01-08 13:10:44 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋