復制代碼 代碼如下:
Function makegroup(teams() As String) As String
'函數功能:對teams數組中的32支球隊進行隨機分組.(注:teams數組下標從0開始)
'函數的變量定義.
'顯示變量聲明是一個好習慣.
Dim tvar As Integer, returnstr As String, tmove As Integer, temp As Integer
'初始化隨機數產生器實現真正的隨機性.
Randomize (Timer)
For tvar = 1 To 32 '共有32支球隊
If (tvar - 1) Mod 4 = 0 Then
'將分組球隊名稱及組名存入returnstr變量中.
returnstr = returnstr & vbCrLf & Chr(Asc("A") + (tvar \ 4)) & "組:"
End If
tmove = Int(Rnd * (33 - tvar))
returnstr = returnstr & teams(tmove) & "."
For temp = tmove To 30
'將數組的部分前移避免組中存在同名球隊.
teams(temp) = teams(temp + 1)
Next temp
Next tvar
'返回值為分組後的結果.
makegroup = returnstr
End Function