VB代码的书写
问下大神:点击按钮后创建一个长度为10的数组,随机生成10个3~100的随机整数保存在数组中。然后找出数组中的素数并打印出来,最后统计素数的个数并打印出来。这段vb代码怎写谢谢
Dim Number(9) As Integer, i As Integer, PrimeNum As Integer Private Function IsPrime(Num As Integer) As Boolean IsPrime = True For i = 2 To Int(Sqr(Num)) + 1 If Num Mod i = 0 Then IsPrime = False : Exit Function Next End Function Private Sub CmdCreate_Click() For i = 0 To 9 Number(i)=Int(Rnd() * (100 - 3)) + 3 If IsPrime(Number(i)) Then Print Number(i) : PrimeNum = PrimeNum + 1 Next Print "素数个数:" & PrimeNum End Sub
Dim A(1 To 10) As Integer, B(1 To 10) As Integer, I As Integer, J As Integer, T As Integer Private Sub Command1_Click() Text1.Text = Text1.Text & vbCrLf & "-------------" & vbCrLf Text1.Text = Text1.Text & "以下是素数:" & vbCrLf For I = 1 To 10 For J = 2 To A(I) - 1 If A(I) Mod J = 0 Then B(I) = B(I) + 1 '检测因子的个数 Next J Next I For I = 1 To 10 If B(I) = 0 Then '如果因子个数为0,说明是素数 T = T + 1 Text1.Text = Text1.Text & A(I) & Space(4) '在文本框显示素数 End If Next I Text1.Text = Text1.Text & vbCrLf & "素数有:" & T & "个" End Sub Private Sub Form_Load() Text1.Text = "" Text1.Text = "生成是10个数:" & vbCrLf For I = 1 To 10 Randomize A(I) = Int(Rnd * 97) + 3 Text1.Text = Text1.Text & A(I) & Space(4) '在文本框显示10个数 Next I End Sub