编写程序,从键盘输入一个数,判断输入的数是否素数。 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