用VB编程,随机抽取两个人的姓名?
答案:2 悬赏:60 手机版
解决时间 2021-03-19 06:34
- 提问者网友:夢醒日落
- 2021-03-18 08:21
从20几个人中随机抽取两个人的姓名,抽取一次后总数减掉2,直到抽取完为此,第二轮又是这么抽取。在VB当中应该如何实现?知道的朋友麻烦告诉我一下,谢谢了!!
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-03-18 08:56
先在窗体上添加一个按钮和一个列表
把以下代码写入按钮的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
全部回答
- 1楼网友:深街酒徒
- 2021-03-18 10:19
你确定是1个硬币,还不是2个硬币?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯