Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, n ch = 0 For i = 2 To n - 1 If n Mod i = 0 Then ch = 1 Next If ch = 0 Then Print #2, "是質數" Else Print #2, "不是質數" Close Close End End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\out.txt" For Output As #2
Open App.Path & "\in.txt" For Input As #1
Input #1, x
If x = 2 Then
a = 2
Else
For i = 2 To x - 1
If x Mod i = 0 Then
a = 1
Exit For
Else
a = 2
End If
Next i
End If
If a = 1 Then Print #2, "不是質數"
If a = 2 Then Print #2, "是質數"
Close #1
Close #2
End
End Sub
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, que
For i = 2 To que - 1
If que Mod i = 0 Then ans = False
Next i
If ans <> False Then Print #2, "是質數" Else Print #2, "不是質數"
Close #2
Close #1
End
End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, a
Close #1
checks = 0
For i = 2 To a / 2
If a Mod i = 0 Then checks = 1: Exit For
Next
Open App.Path & "\out.txt" For Output As #2
If checks = 0 Then Print #2, "是質數" Else Print #2, "不是質數"
Close #2
End
End Sub
緣尉、hummer,arro好,
回覆刪除三個程式都正確。但是arro最好jimmer次之。
熊掌ipad
Dim A As Integer
回覆刪除Dim P As Boolean
Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Input #1, A
Close #1
For i = 2 To (A - 1)
If A Mod i = 0 Then P = True: Exit For
Next i
Open App.Path & "\out.txt" For Output As #2
If P = True Then
Print #2, "不是質數"
Else
Print #2, "是質數"
End If
Close #2
End
End Sub
佑好,
回覆刪除程式正確,變數p的初值,設一下比較好。
熊掌
老師好,
回覆刪除因為學到boolean 預設為false
下次會注意,謝謝
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
ch = 0
For i = 2 To n - 1
If n Mod i = 0 Then ch = 1
Next
If ch = 0 Then Print #2, "是質數" Else Print #2, "不是質數"
Close
Close
End
End Sub
1:33