VB可以自定义一个求素数的函数来实现输出一定范围内的素数。
Function 语句,声明 Function 过知改程的名称,参数以及构成其主体的代码。
示例代码,求1000以内的素数:
Option Explicit
Dim I As Integer
Dim j As Long
Private Function IsPrime(ByVal n As Integer) As Boolean
On Error Resume Next
伍模 Dim I As Integer
Dim temp As Integer
Dim flag As Boolean
flag = True
If (n <= 1) Then
IsPrime = False
End If
For I = 2 To Sqr(n)
If (n Mod I = 0) Then
flag = False
Exit For
End If
Next I
IsPrime = flag
End Function
Private Sub Form_Click()
For I = 1 To 1000
Call IsPrime(I)
If IsPrime(I) = True 腔猛缓Then
Print I;
j = j + 1
If j Mod 10 = 0 Then
End If
End If
Next I
End Sub
楼主的上半段程序也有错缓册,已经修改完成了。请注意对比。扰此宏
Private Sub Command1_Click()
Dim i As Integer, j As Integer
j = 0
For i = 2 To 100
If sushu(i) = True Then
Print i;
j = j + 1
If j Mod 5 = 0 Then Print
End If
Next i
End Sub
Function sushu(m As Integer) As Boolean
sushu = True
For i = 2 To Sqr(m)
扒蔽 If m Mod i = 0 Then sushu = False: Exit For
Next i
End Function