求两数之间的素数,可是循环终止不了,代码如下:

来源:百度知道 编辑:UC知道 时间:2024/05/09 06:16:45
private sub command1_click()
dim a%,b%,n%
a=inputbox("请输入整数a:")
b=inputbox("请输入整数b(b>a):")
n = a
m = 0
Do Until n > b
c = 0
For i = 2 To Sqr(n)
If n Mod i = 0 Then
c = 1
Exit For
End If
Next i
If c = 0 Then
Print n;
m = m + 1
End If
If m = 10 Then
m = 0
Print
End If
n = n + 1
Loop
End Sub
用子程序实现代码循环终止不了:

Sub ss(n, a, b)
n = a
m = 0
Do Until n > b
c = 0
For i = 2 To Sqr(n)
If n Mod i = 0 Then
c = 1
Exit For
End If
Next i
If c = 0 Then
Print n;
m = m + 1
End If
If m = 10 Then
m = 0
Print
End If
n = n + 1
Loop
End Sub

Private Sub Command1_Click()
Dim a, b, n As Integer
a=inputbox("请输入整数a:")
b=inputbox("请输入整数b(b>a):")

Call ss(n, a, b)

End Sub

运行的很正常呀。循环能正常终止的呀!
按了command1后,程序是不会自动终止的,再按command1后,可以输入新的两个数计算。

要关闭Form才能中止退出!

Sub ss(n%, a%, b%)
n = a
m = 0
Do Until n > b
c = 0
For i = 2 To Sqr(n)
If n Mod i = 0 Then
c = 1
Exit For
End If
Next i
If c = 0 Then
Print n;
m = m + 1
End If
If m = 10 Then
m = 0
Print
End If
n = n + 1
Loop
End Sub

Private Sub Command1_Click()
Dim a%, b%, n%
a = InputBox("请输入整数a:")
b = InputBox("请输入整数b(b>a):")

Call ss(n, a, b)

End Sub

类型不匹配的缘故!

我测试了一下,没问题。能正确输出的。