|
Sub Lotto() Const spins = 6
Const minNum = 1
Const maxNum = 51
Dim t As Integer '外部循环自变量
Dim i As Integer '内部循环自变 量
Dim myNumbers As String
Dim lucky(spins) As String
myNumbers = ""
For t = 1 To spins
Randomize
lucky(t) = Int((maxNum-minNum+1) * Rnd )+ minNum
For i = 1 To (t-1)
If lucky(t)=lucky(i) Then
lucky(t) = Int((maxNum–minNum+1) * Rnd)+ minNum
i = 0
End If
Next i
MsgBox "并运号码是" & t & lucky(t)
myNumbers = myNumbers & " –" & lucky(t)
Next t
MsgBox "并运号码是" & myNumbers
End Sub
这是一段产生6个随机数的代码,要求6个数不能重复,其中那个嵌套循环没看懂 ,请大家帮忙理解一下
For i = 1 To (t-1)
If lucky(t)=lucky(i) Then
lucky(t) = Int((maxNum–minNum+1) * Rnd)+ minNum
i = 0
End If
Next i
” lucky(t) = Int((maxNum–minNum+1) * Rnd)+ minNum“这个语句的意思是不是 如果新产生的数字与前面某一个数字相同的话就再新生成一个随机数?还有 i = 0 这个有什么用呢 ?请大家解释得详细一点儿,初学者,谢谢。
|
|