VB程序题目急求 求100到1000间的所有质数,每行输出6个,分多行输出。
展开全部
'新建一个工程。把Form窗口拉长一点。在代码窗口写入如下代码:
Option Explicit
Private Function IsPrimeNumber(ByVal Number As Integer) As Boolean
Dim i As Integer
For i = 2 To Number - 1
If Number Mod i = 0 Then
IsPrimeNumber = False '非素数
Exit Function
End If
Next i
IsPrimeNumber = True '素数
End Function
Private Sub Form_Click()
Dim Num As Integer
Dim Count As Integer
Dim OutPut As String
Count = 0
OutPut = ""
For Num = 100 To 1000
If IsPrimeNumber(Num) Then '如果是质数则保存到输出队列
If Count < 6 Then
OutPut = OutPut & Num & " "
Count = Count + 1
Else
Print OutPut '输出队列已满6个,则输出
OutPut = ""
OutPut = OutPut & Num & " "
Count = 1
End If
End If
Next Num
If Count < 6 Then
Print OutPut
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询