抽奖规则:中奖人数为1人,男女不限:
中奖人数超1人,男女对半(奇数时女性多1人)。
程序运行界面如图所示。
实现上述功能的VB程序如下:
Dim xm(1 To 500) As String, xh(1 To 500) As String, xb(1 To 500) As String
Dim n As Integer '学生总人数
Dim a(1 To 100) As Integer '抽中序号
Private Sub Form_ LoadO
'本过程读取n位同学的序号、姓名和性别,
'分别存储在数组xh、xm、xb中
并在列表框List1中显示,代码略
End Sub
Private Sub Command1_ ClickO
Dim rs As Integer, m As Integer, x As Integer, girl As Integer, I As Integer
Dim xbs(1 To 500) As Integer
Randomize
List2.Clear
List2.AddItem "姓名" & " " & "性别"
rs= ①
For i=1 To n
If xb(i)="女" Then xbs<(i)=1 Else xbs(i)=0
Next i
m=0
girl=0
If rs= 1 Then
x= Int(Rnd* n)+ 1
List2.AddItem xm(x) &" "&xb(x)
Else
Do While m<rs
x = Int(Rnd* n)+ 1
If checkf(x) Then
m=m+1: a(m)=x
girl = girl + xbs(a(m))
If ② Then
a(m)=0: m=m-1
ElseIf girl> (rs+1)\2 Then
a(m)=0: m=m-1
③
End If
End If
Loop
For i=1 To m
List2.AddItem
Next i
End If
End Sub
Function checkf(p As Integer) As Boolean
'核对重复中奖,未中过奖返回True
'代码略
End Function
① ② ③