Dim i As Integer, j As Integer, n As Integer
Dim b As Boolean
n = 0
For i = 100 To 999
b = True
For j = 2 To i / 2
If i Mod j = 0 Then
b = False
Exit For
End If
Next j
If b Then n = n + 1
Next i
MsgBox "有" & n & "个质数"
【最佳答案】的这段代码是错的
运行结果是显示100到999的所有整数
改正方法:
在j=i后面加一行:exit for
或者把j<>i改为j>i
如下图