谁能给我个VB的随机抽取器的源码? 类似小飞侠随机抽取器那样,只要能抽取人名就可以了
人名要从TXT文件导入,有两个textbox,一个显示TXT文件中的名字并滚动,另一个显示随机抽取的结果。 还有两个按钮,一个开始,一个停止
谁能给我个VB的随机抽取器的源码?
答案:3 悬赏:80 手机版
解决时间 2021-02-11 03:48
- 提问者网友:喧嚣尘世
- 2021-02-10 07:41
最佳答案
- 五星知识达人网友:鱼芗
- 2021-02-10 08:31
代码如下,已测试(注意:其中有个text3.text,把它替换成txt文件路径即可,别忘了加引号。另外txt中的人名要求每两个之间用空格或回车分开)
Dim Flag As Boolean
Private Sub Command1_Click() '--------开始
Dim Str1 As String, Str0 As String, n As Integer
Flag = True
ff = FreeFile
'------------------------读取姓名文档
Open Trim$(Text3.Text) For Input As #ff
Do Until EOF(ff)
Input #ff, Str0
Str1 = Str1 + " " + Str0
Loop
Close #ff
'------------------------剔除空格
stra = Split(Str1, " ")
Str1 = ""
For i = LBound(stra) To UBound(stra)
If stra(i) <> "" Then
Str1 = Str1 + stra(i) + " "
End If
Next
stra = Split(Str1, " ")
n = UBound(stra)
'-----------------------开始随机抽取
Randomize
Do Until Flag = False
Text1.Text = stra(Int(Rnd * n))
DoEvents
Loop
Print n
End Sub
Private Sub Command2_Click() '--------停止
Flag = False
Text2.Text = Text1.Text
End Sub
Dim Flag As Boolean
Private Sub Command1_Click() '--------开始
Dim Str1 As String, Str0 As String, n As Integer
Flag = True
ff = FreeFile
'------------------------读取姓名文档
Open Trim$(Text3.Text) For Input As #ff
Do Until EOF(ff)
Input #ff, Str0
Str1 = Str1 + " " + Str0
Loop
Close #ff
'------------------------剔除空格
stra = Split(Str1, " ")
Str1 = ""
For i = LBound(stra) To UBound(stra)
If stra(i) <> "" Then
Str1 = Str1 + stra(i) + " "
End If
Next
stra = Split(Str1, " ")
n = UBound(stra)
'-----------------------开始随机抽取
Randomize
Do Until Flag = False
Text1.Text = stra(Int(Rnd * n))
DoEvents
Loop
Print n
End Sub
Private Sub Command2_Click() '--------停止
Flag = False
Text2.Text = Text1.Text
End Sub
全部回答
- 1楼网友:青尢
- 2021-02-10 10:40
Private Sub Form_click()
Dim a(3) As String
a(0) = "小王"
a(1) = "小李"
a(2) = "小谢"
a(3) = "小林"
Randomize
MsgBox a(Int(Rnd * 4)), vbOKOnly, "标题"
End Sub
'不用添加任何控件 直接将代码复制进去 点击窗体即可得到效果
- 2楼网友:患得患失的劫
- 2021-02-10 09:24
首先你要有一个.txt文本格式的人员名单,一行一个人名,然后在下面的程序中更改名单路径、抽取人数就可以了
option explicit
private sub command1_click()
dim namearr(), mpath$, n%, k%, newdic, temp$
mpath = "d:\name.txt" '原始名单路径,每行一人
k = int(inputbox("抽取的人数:")) '抽取的数目
'加载原始名单
open mpath for input as #1
do while not eof(1)
n = n + 1
redim preserve namearr(1 to n)
line input #1, namearr(n)
loop
close #1
'抽取人员
set newdic = createobject("scripting.dictionary")
do while newdic.count < k
randomize
temp = namearr(int(rnd * (ubound(namearr) + 1)))
if newdic.exists(temp) = false then newdic.add temp, ""
loop
'输出
temp = join(newdic.keys, ", ")
print n & "名人员中," & k; "名被抽取,名单如下:" & vbcrlf & temp
newdic = ""
end sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯