用VB编程,随机抽取两个人的姓名?
从20几个人中随机抽取两个人的姓名,抽取一次后总数减掉2,直到抽取完为此,第二轮又是这么抽取。在VB当中应该如何实现?知道的朋友麻烦告诉我一下,谢谢了!!...
从20几个人中随机抽取两个人的姓名,抽取一次后总数减掉2,直到抽取完为此,第二轮又是这么抽取。在VB当中应该如何实现?知道的朋友麻烦告诉我一下,谢谢了!!
展开
展开全部
先在窗体上添加一个按钮和一个列表
把以下代码写入按钮的Click事件中,单击按钮就可以看到运行结果
Private Sub Command1_Click()
Dim NameList As New Collection
'把参与抽取的名字添加到名字集合中
NameList.Add "姓名01"
NameList.Add "姓名02"
NameList.Add "姓名03"
NameList.Add "姓名04"
NameList.Add "姓名05"
NameList.Add "姓名06"
NameList.Add "姓名07"
NameList.Add "姓名08"
NameList.Add "姓名09"
NameList.Add "姓名10"
NameList.Add "姓名11"
NameList.Add "姓名12"
NameList.Add "姓名13"
NameList.Add "姓名14"
NameList.Add "姓名15"
NameList.Add "姓名16"
NameList.Add "姓名17"
NameList.Add "姓名18"
NameList.Add "姓名19"
NameList.Add "姓名20"
NameList.Add "姓名21"
Dim StrGetName As String
Dim a As Integer
Randomize
List1.Clear
Do
If NameList.Count > 0 Then
a = Int(Rnd * NameList.Count) + 1
If StrGetName <> "" Then
StrGetName = StrGetName & " " & NameList.Item(a)
List1.AddItem StrGetName
StrGetName = ""
Else
StrGetName = NameList.Item(a)
End If
NameList.Remove a
Else
If StrGetName <> "" Then List1.AddItem StrGetName
Exit Do
End If
Loop
End Sub
把以下代码写入按钮的Click事件中,单击按钮就可以看到运行结果
Private Sub Command1_Click()
Dim NameList As New Collection
'把参与抽取的名字添加到名字集合中
NameList.Add "姓名01"
NameList.Add "姓名02"
NameList.Add "姓名03"
NameList.Add "姓名04"
NameList.Add "姓名05"
NameList.Add "姓名06"
NameList.Add "姓名07"
NameList.Add "姓名08"
NameList.Add "姓名09"
NameList.Add "姓名10"
NameList.Add "姓名11"
NameList.Add "姓名12"
NameList.Add "姓名13"
NameList.Add "姓名14"
NameList.Add "姓名15"
NameList.Add "姓名16"
NameList.Add "姓名17"
NameList.Add "姓名18"
NameList.Add "姓名19"
NameList.Add "姓名20"
NameList.Add "姓名21"
Dim StrGetName As String
Dim a As Integer
Randomize
List1.Clear
Do
If NameList.Count > 0 Then
a = Int(Rnd * NameList.Count) + 1
If StrGetName <> "" Then
StrGetName = StrGetName & " " & NameList.Item(a)
List1.AddItem StrGetName
StrGetName = ""
Else
StrGetName = NameList.Item(a)
End If
NameList.Remove a
Else
If StrGetName <> "" Then List1.AddItem StrGetName
Exit Do
End If
Loop
End Sub
追问
我希望有一个抽取时的一个闪烁的姓名的变动,把抽取的两个人的姓名显示在某个位置,并且抽取一次下一次就不在重复,直到所有人都抽完了,再重新开始抽,还有你能够完整的写一个吗?万分感谢!!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询