
vb作业,随机生成20个100以内的数,然后去掉非质数。求告知哪里错了。 Public Class 20
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, j, flag As Integer
Dim a(19) As Integer
For i = 0 To 19
a(i) = Rnd() * 99 + 1
Next
i = 0
Do While (i <= UBound(a))
flag = 0
For j = 2 To Int(System.Math.Sqrt(a(i)))
If (a(i) Mod j) = 0 Then
a(i) = a(UBound(a))
i -= 1
ReDim Preserve a%(UBound(a) - 1)
End If
Next
i += 1
Loop
For i = 0 To UBound(a)
Label1.Text &= a(i) & vbCrLf
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = ""
End Sub
End Class 展开
Private Sub Command1_Click()
Dim i, j, k As Integer
Dim a(19) As Integer
Label1.Caption = ""
Randomize
Form1.Cls
For i = 0 To 19
a(i) = Rnd() * 99 + 1
Print a(i);
If i Mod 10 = 9 Then Print
Next
i = 0
For i = 0 To 19
For j = 2 To Int(Sqr(a(i)))
If (a(i) Mod j) = 0 Then Exit For
Next
If a(i) > 1 And a(i) Mod j > 0 Then a(k) = a(i): k = k + 1
Next i
For i = 0 To k - 1
Label1.Caption = Label1.Caption & a(i) & vbCrLf
Next
End Sub