1
我想生成9個隨機數。然後我會從這9個隨機數中得到4個,但是四個隨機條目*不能重複。 有28個可能的結果。隨機化工作正常,但我不明白爲什麼我不能讓複選框得到檢查。我一直在嘗試了一整天,請幫助從9箇中選擇四個隨機數而不重複
Dim rn As New Random
Dim n(9) As Integer
n(1) = rn.Next(1, 2)
n(2) = rn.Next(1, 3)
n(3) = 1
n(4) = rn.Next(1, 3)
n(5) = 1
n(6) = rn.Next(1, 4)
n(7) = rn.Next(1, 7)
n(8) = rn.Next(1, 4)
n(9) = rn.Next(1, 3)
Dim loopcount As Integer = 4
Dim l(4), ln, lm As Integer
ln = 1
lm = 1
l(1) = rn.Next(1, 9)
l(2) = rn.Next(1, 9)
While l(2) = l(1)
l(2) = rn.Next(1, 9)
End While
l(3) = rn.Next(1, 9)
While l(3) = l(2) Or l(3) = l(1)
l(3) = rn.Next(1, 9)
End While
l(4) = rn.Next(1, 9)
While l(4) = l(3) Or l(4) = l(2) Or l(4) = l(1)
l(3) = rn.Next(1, 9)
End While
MsgBox(l(1) & l(2) & l(3) & l(4))
MsgBox(n(1) & n(2) & n(3) & n(4) & n(5) & n(6) & n(7) & n(8) & n(9))
While loopcount > 0
If ln = 1 Then lm = l(1)
If ln = 2 Then lm = l(2)
If ln = 3 Then lm = l(3)
If ln = 4 Then lm = l(4)
If l(ln) = 1 & n(lm) = 1 Then 1.CheckState = 1
If l(ln) = 1 & n(lm) = 2 Then 2.CheckState = 1
If l(ln) = 2 & n(lm) = 1 Then 3.CheckState = 1
If l(ln) = 2 & n(lm) = 2 Then 4.CheckState = 1
If l(ln) = 2 & n(lm) = 3 Then 5.CheckState = 1
If l(ln) = 3 & n(lm) = 1 Then 6.CheckState = 1
If l(ln) = 4 & n(lm) = 1 Then 7.CheckState = 1
If l(ln) = 4 & n(lm) = 2 Then 8.CheckState = 1
If l(ln) = 4 & n(lm) = 3 Then 9.CheckState = 1
If l(ln) = 5 & n(lm) = 1 Then 10.CheckState = 1
If l(ln) = 6 & n(lm) = 1 Then 11.CheckState = 1
If l(ln) = 6 & n(lm) = 2 Then 12.CheckState = 1
If l(ln) = 6 & n(lm) = 3 Then 13.CheckState = 1
If l(ln) = 6 & n(lm) = 4 Then 14.CheckState = 1
If l(ln) = 7 & n(lm) = 1 Then 15.CheckState = 1
If l(ln) = 7 & n(lm) = 2 Then 16.CheckState = 1
If l(ln) = 7 & n(lm) = 3 Then 17.CheckState = 1
If l(ln) = 7 & n(lm) = 4 Then 18.CheckState = 1
If l(ln) = 7 & n(lm) = 5 Then 19.CheckState = 1
If l(ln) = 7 & n(lm) = 6 Then 20.CheckState = 1
If l(ln) = 7 & n(lm) = 7 Then 21.CheckState = 1
If l(ln) = 8 & n(lm) = 1 Then 22.CheckState = 1
If l(ln) = 8 & n(lm) = 2 Then 23.CheckState = 1
If l(ln) = 8 & n(lm) = 3 Then 24.CheckState = 1
If l(ln) = 8 & n(lm) = 4 Then 25.CheckState = 1
If l(ln) = 9 & n(lm) = 1 Then 26.CheckState = 1
If l(ln) = 9 & n(lm) = 2 Then 27.CheckState = 1
If l(ln) = 9 & n(lm) = 3 Then 28.CheckState = 1
ln = ln + 1
loopcount = loopcount - 1
End While
Call Generate()
感謝您的答覆,複選框有不同的名稱。我只是將它們改爲數字,因爲它們很長,並顯示它們是28,但如果它有幫助,我可以在原始代碼中提供原始名稱。 – Jan
@Jan:對不起 - 剛更新了我的答案。 – Paul
謝謝你會馬上嘗試 – Jan