用VB编写“投注”程序
首先,新建一个单窗体的工程,在上面画出七个TEXTBOX,最好是一个控件数组,这样编程时容易控制,再建一个COMMOND BUTTON,将CAPTION改为“随机产生”。在程序运行后,每点击一下COMMAND1,将随机产生一组数字并按从小到大的顺序显示在文本框中。下面就是程序部分:
Dim NumArray(1 To 7) As Integer′通用中定义
Private Sub Command1_Click()
Dim i,j,N As Integer
For i = 1 To 7
NumArray(i) = 0
Next i
Randomize
NumArray(1) = Fix(1 + 32 * (Rnd()))
j = 1
Do
N = Fix(1 + 32 * (Rnd()))
For i = 1 To j
If N = NumArray(i) Then
Exit For ′重复时
ElseIf i = j Then ′未重复时
NumArray(i + 1) = N
j = j + 1
Exit For
End If
Next i
Loop While j < 7
PopSort ′升序排列
For i = 1 To 7
Text1(i - 1).Text = NumArray(i)
Next i
End Sub
Private Sub PopSort() ′气泡排序法
Dim i,j,Temp As Integer
For i = 7 To 2 Step -1
For j = 7 - 1 To 1 Step -1
If i >= 7 - j + 1 Then
If NumArray(j + 1) < NumArray(j) Then
Temp = NumArray(j)
NumArray(j) = NumArray(j + 1)
NumArray(j + 1) = Temp
End If
End If
Next j
Next i
End Sub
本程序在VB6.0中文企业版、Win98SE中文版下调试通过。为了简捷起见,这个程序还有很多不尽如人意的地方,比如不能保存等,喜欢编程的朋友可以充分发挥自己的聪明才智对其加以扩充,有兴趣的朋友可给我发E-MAIL:dangbochang@263.net。