从100到999的质数vb怎样写程序

从100到999的质数vb怎样写程序
2025-01-04 18:44:49
推荐回答(3个)
回答1:

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 & "个质数"

回答2:

【最佳答案】的这段代码是错的
运行结果是显示100到999的所有整数
改正方法:
在j=i后面加一行:exit for
或者把j<>i改为j>i

回答3:

如下图