VB程序中在list1中随机产生50个三位数,并将其中的素数移动到list2
Option Explicit
Private Sub Command1_Click()
List1.Clear: List2.Clear
Call NewNum(List1)
Call ChooseTo(List1, List2)
End Sub
Function NewNum(mList As ListBox)
Dim i As Integer, temp As Integer
For i = 1 To 50
Randomize
temp = Rnd * 1000
If temp > 99 Then
Call mList.AddItem(temp, i - 1)
Else
i = i - 1
End If
Next
End Function
Function ChooseTo(mList1 As ListBox, mList2 As ListBox)
Dim temp As String, i As Integer
For i = 0 To mList1.ListCount - 1
If ChackSUSU(mList1.List(i)) Then mList2.AddItem mList1.List(i)
Next
End Function
Function ChackSUSU(i As Integer) As Boolean
On Error GoTo L_end
Dim j As Integer
For j = 2 To i / 2
If i Mod j = 0 Then ChackSUSU = False: Exit Function
Next
ChackSUSU = True
Exit Function
L_end:
ChackSUSU = False
End Function
是这个不?看是不是你需要的?
Dim n
n = 49
Do
For i = 0 To n
If Not prime(Val(List1.List(i))) Then
List2.AddItem List1.List(i)
List1.RemoveItem i
n = n - 1
Exit For
End If
Next
If i = n + 1 Then Exit Do
Loop
End Sub
Private Sub Form_Load()
For i = 0 To 49
List1.List(i) = Int(Rnd * 900 + 100)
Next
End Sub
Private Function prime(ByVal x%) As Boolean
Dim f As Boolean
For i = 2 To Sqr(x)
If x Mod i = 0 Then f = True
Next
prime = f
End Function