会计事务所排名2017:怎样判断一个数是否为质数用VB语言

来源:百度文库 编辑:高校问答 时间:2024/04/26 21:41:28
用VB语言源代码

在窗体上画个文本框TEXT1,multi属性改为TRUE

Private Sub Form_Load()
Dim ArrayInt() As Integer
Dim CheckNum As Integer
Dim PrimeNo As Integer
CheckNum = Val(InputBox("输入一个数", "质数判断"))
PrimeNo = 1
ReDim Preserve ArrayInt(PrimeNo) As Integer
ArrayInt(1) = 2
For j = 3 To CheckNum
Flag = j
For I = 1 To PrimeNo
If (j Mod ArrayInt(I) = 0) Then
Flag = j - 1
Exit For
End If
Next I
If (Flag = j) Then
PrimeNo = PrimeNo + 1
ReDim Preserve ArrayInt(PrimeNo)
ArrayInt(PrimeNo) = j
End If
Next j
Text1.Text = ""
Text1.Text = Text1.Text & "在" & CheckNum & "之内的质数共有" & PrimeNo & "个,分别为" + vbCrLf
For I = 1 To PrimeNo
Text1.Text = Text1.Text & I & "th " & ArrayInt(I) & vbCrLf
Dim Message As String
Message = Str(CheckNum)
If (CheckNum = ArrayInt(I)) Then
Message = Message & " 是质数"
Else
Message = Message & " 不是质数!"
End If
Next I
MsgBox Message, vbOKOnly, "质数判断"

End Sub