代码如下:
Option Explicit
Dim xx As Long
Public Function sus(ByVal x As Long) As Boolean
'素数
Dim i As Long
sus = True
For i = 2 To Math.Sqr(x)
If x Mod i = 0 Then sus = False: Exit For
Next i
End Function
Private Sub Command1_Click()
If sus(xx) Then
Label1.Caption = Text1.Text & " 是素数"
Else
Label1.Caption = Text1.Text & " 是合数"
End If
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
On Error Resume Next
If Text1.Text = vbNullString Then
Cancel = True
Exit Sub
End If
xx = CLng(Text1.Text)
If xx <= 0 Or xx <> Fix(Text1.Text) Then
MsgBox "请输入小于2147483647正整数", vbOKOnly Or vbInformation, Me.Caption
Cancel = True
End If
End Sub