求vb程序:从数字1-30中随机选取不重复的10个,并输出?
发布网友
发布时间:2024-09-30 20:09
我来回答
共2个回答
热心网友
时间:2024-10-26 23:17
Dim a() As Integer, i As Integer, b(1 To 30) As String, k As Integer
For i = 1 To 30
b(i) = Trim(Str(i))
Next i
ReDim a(1 To 30)
For i = 1 To 30
a(i) = i
Next i
Do While UBound(a) > 20
Randomize
k = Int(Rnd() * (UBound(a)) + 1)
List1.AddItem b(a(k))
If k < UBound(a) Then
For i = k To UBound(a) - 1
a(i) = a(i + 1)
Next i
End If
ReDim Preserve a(1 To UBound(a) - 1)
Loop
用到的控件:一个按钮,一个lsitbox。
把程序中的30改为20就行。
热心网友
时间:2024-10-26 23:18
Dim list As New System.Collections.ArrayList
Dim val As Integer
Dim ran As Random = New Random
While list.Count < 5
val = ran.Next(1, 30)
If Not list.Contains(val) Then
list.Add(val)
End If
End While
Dim obj As Object
For Each obj In list
MessageBox.Show(obj)
Next