程序界面:
代码:
Private Sub Command1_Click()
'判断用户输入是否合法(大于2的偶数)
If IsNumeric(Text1.Text) = False Or Val(Text1.Text) <= 2 Or Val(Text1.Text) Mod 2 <> 0 Then
MsgBox "请输入一个大于2的偶数"
Text1.Text = ""
Text1.SetFocus
Exit Sub
End If
Dim j As Integer
Dim t As Integer
t = Val(Text1.Text)
'若j和t-j均为素数,则说明t可以分解为两个素数之和
For j = 2 To t - 2
If SuShu(j) = True And SuShu(t - j) = True Then
Text2.Text = CStr(j)
Text3.Text = CStr(t - j)
Exit For
End If
Next j
End Sub
'判断参数a是否为素数,若是素数,返回True;否则返回False
Private Function SuShu(a As Integer) As Boolean
Dim i As Integer
For i = 2 To a - 1
If a Mod i = 0 Then Exit For
Next i
If i = a Then
SuShu = True
Else
SuShu = False
End If
End Function