-----------提供两组代码,共同学习,呵呵。----------
反正我也是初学,共同学习吧,这个答案中,除了Boolean假设法不会用,其余均满足了题目要求,敬请知道的朋友参考给出答案。
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim s As String
Dim s1 As String
Dim a() As Integer
Private Sub Command1_Click()
k = 0
ReDim Preserve a(100)
For i = 100 To 200
For j = 2 To Sqr(i)
If i Mod j = 0 Then Exit For
Next j
If j > Sqr(i) Then
a(k) = i
k = k + 1
End If
Next i
k = k - 1
ReDim Preserve a(k)
For j = 0 To k
s1 = CStr(a(j)) & Space$(1)
s = s & s1
If (j + 1) Mod 5 = 0 Then s = s & vbCrLf
Next j
Text1.Text = s
End Sub
----------------------------------------
----------------------------------------
又仔细看了下网上大家关于素数的算法,重新修改如下,估计这段代码是你需要的:
'画个命令按钮,画个文本框,之后把文本框的MultiLine属性设为True以进行换行。用下面的代码:'
Private Sub Command1_Click()
Dim b As Boolean
Dim i, j, k As Integer
Dim s, s1 As String
For i = 100 To 200
DoEvents
b = True
For j = 2 To i - 1
If i Mod j = 0 Then
b = False
Exit For
End If
Next j
If b Then
k = k + 1
s1 = i & Space$(1)
s = s & s1
If k Mod 5 = 0 Then s = s & vbCrLf
End If
Next i
Text1.Text = s
End Sub
Private Sub Form_Load()
Text1.Text = ""
k = 0
End Sub