English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

我想請問 假如我設計一個亂數程式

例如樂透彩的49號選7個出來

但是我設計出來後同組7個數字中

有時候會重複出現呢

那請問要如何更改程式碼??

多謝喔

2006-12-28 17:08:39 · 3 個解答 · 發問者 Roy 2 in 電腦與網際網路 程式設計

3 個解答

Private Sub Command1_Click()
Dim i As Integer, j As Integer, a(49) As Integer

Randomize
For i = 1 To 49
a(i) = 0 ' 全部未出現過
Next i
For i = 1 To 6
Do
j = (Int(Rnd() * 49) + 1)
Loop While a(j) = 1 ' 找到未出現過的才停止
a(j) = 1 ' 標示此號已經出現過
print j ' 印出來
Next i

End Sub

如果有問題, 請來函討論. 不然, 我可能會錯失你再補充的疑點.

2006-12-29 01:01:40 · answer #1 · answered by JJ 7 · 0 0

dim 陣列(7)
randomize
個數 = 0
do while 個數 <= 6
x=亂數取整數
if x在1和49之間 then
相等 = 假
若陣列元素 = x 則 相等 = 真
若相等 = 假 則
陣列(個數) = x
print 陣列(個數) & " "
個數 = 個數 + 1
end if
end if
loop

2006-12-28 18:47:50 · answer #2 · answered by yoo 3 · 0 0

在表單上放入Command1,Text1改成txtA,將txtA用複製貼上的方法做控制項陣列txtA(0)~txtA(6),依序排好後,將下列程式寫入。
Dim sNumber(49) As Integer
Private Sub Command1_Click()
Call lottle
For i = 0 To 6
txtA(i) = sNumber(i + 1)
Next
End Sub
Private Sub lottle()
Dim intRnd As Integer
For i = 1 To 49
sNumber(i) = i
Next
For i = 1 To 49
intRnd = CInt(Rnd() * 49)
Call SwapElement(sNumber(i), sNumber(intRnd))
Next
End Sub
Private Sub SwapElement(ByRef varItem1 As Variant, ByRef varItem2 As Variant)
Dim varTemp As Variant
varTemp = varItem1
varItem1 = varItem2
varItem2 = varTemp
End Sub

2006-12-28 17:59:35 · answer #3 · answered by 水月 6 · 0 0

fedest.com, questions and answers