vb编写程序,从键盘输入一个数,判断输入的数是否素数。

编写程序,从键盘输入一个数,判断输入的数是否素数。
Dimn as long,f as boolean
N=val(inputbox(“请输入要判断的数:”))
F=true
Fori=2 to sqr(n)
If n mod i=0 then
F=false
exit for
end if
Nexti
Iff=true then
print n;”是素数”
Else
print n;”不是素数”
End if
end sub
这样编写对吗?

Private Sub Command1_Click()
    Dim n As Long
    n = Val(InputBox("请输入要判断的大于0的数:"))
    If n < 1 Then MsgBox "你输入的是0": Exit Sub
    If zhi(n) Then MsgBox "是素数" Else MsgBox "不是素数"
End Sub
Private Function zhi(x As Long) As Boolean
    Dim b As Single, i As Long, f As Long
    For i = 2 To x - 1
        If x Mod i = 0 Then
            f = 1: Exit For
        End If
    Next
    If f = 1 Then zhi = False Else zhi = True
End Function
温馨提示:答案为网友推荐,仅供参考
相似回答