伊莉討論區
標題:
如何抽完號後進行第二輪
[打印本頁]
作者:
fmp10273
時間:
2015-6-25 05:32 PM
標題:
如何抽完號後進行第二輪
本帖最後由 fmp10273 於 2015-6-26 09:32 AM 編輯
同上,最近要做類似範圍內抽號碼的程式..
Dim x As Integer
Static I As Integer
Randomize()
I = I + 1
If TextBox2.Text = "" Then
MsgBox("請輸入參加人數")
Else
x = Int((Rnd() * TextBox2.Text)) + 1
Label1.Text = Str(x)
Label2.Text = I
TextBox1.Text &= Str(x) & vbCrLf
End If
複製代碼
抽出的號碼顯示在TextBox1裡...但抽出的號碼還要在做第二輪抽號..
如何從TextBox1裡的指定號碼再抽出裡面的隨機號?
作者:
johnwanz
時間:
2015-6-26 11:18 AM
我個人的想法會是, 第一輪的結果作為Array或List, 第二輪抽號碼, 以index為範疇, 輸出時再將index套入第一輪的結果.
作者:
fmp10273
時間:
2015-6-27 05:05 PM
本帖最後由 fmp10273 於 2015-6-29 05:57 PM 編輯
陣列是可以取前10個了..但陣列亂數就不知該怎樣用了
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim a(100) As Integer
Dim r(100) As Integer
Dim i As Integer
Dim j As Integer
Randomize()
Dim t As Integer = TextBox2.Text
For i = 1 To t
a(i) = i
Next i
For i = t To 1 Step -1
j = Int(Rnd() * i) + 1
r(i) = a(j)
a(j) = a(i)
Next i
For i = 1 To t
TextBox1.Text &= r(i) & vbCrLf
Next i
For i = 1 To 50
myArray(i) = r(i)
TextBox4.Text &= myArray(i) & vbCrLf
Next
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
Dim i As Integer
Randomize()
For i = 1 To 10
myA(i) = myArray(i)
TextBox3.Text &= myA(i) & vbCrLf
Next
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
Dim a(10) As Integer
Dim r(10) As Integer
Dim i As Integer
Dim j As Integer
Dim k As String
For i = 1 To 10
myA(i) = i
Next i
For i = 10 To 1 Step -1
j = Int(Rnd() * i) + 1
myA(i) = myA(j)
myA(j) = myA(i)
Next i
For i = 1 To 10
TextBox3.Text &= myA(i) & vbCrLf
Next i
End Sub
複製代碼
http://i.imgur.com/XWGxUeg.png
作者:
rr09192084
時間:
2015-7-1 02:40 PM
本帖最後由 rr09192084 於 2015-7-1 02:45 PM 編輯
Public Class Form1
REM 宣告三個System.Collections.ArrayList類別的容器來放置亂數號碼
Private arrayA As System.Collections.ArrayList = New System.Collections.ArrayList()
Private arrayB As System.Collections.ArrayList = New System.Collections.ArrayList()
Private arrayC As System.Collections.ArrayList = New System.Collections.ArrayList()
REM </summary>
REM 產生不重複的亂數
REM </summary>
REM <param name="intLower"></param>產生亂數的範圍下限
REM <param name="intUpper"></param>產生亂數的範圍上限
REM <param name="intNum"></param>產生亂數的數量
REM <param name="isSort"></param>是否排序,(這個參數可省略,預設值是否)
Private Function MakeRand(intLower As Integer, intUpper As Integer, _
intNum As Integer, Optional isSort As Boolean = False) _
As System.Collections.ArrayList
Dim arrayRand As New System.Collections.ArrayList()
Dim random As New System.Random()
Dim intRnd As Integer
While arrayRand.Count < intNum
intRnd = random.[Next](intLower, intUpper + 1)
If Not arrayRand.Contains(intRnd) Then
arrayRand.Add(intRnd)
End If
End While
If isSort Then
arrayRand.Sort()
End If
Return arrayRand
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
REM <<<<<第一輪抽獎程式>>>>>
REM 先清空容器
arrayA.Clear()
arrayB.Clear()
REM 再從1到500號中抽出30個中獎號碼放到 arrarA 中
arrayA = MakeRand(1, 500, 30)
REM 清空文字框
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
REM 因為 System.Collections.ArrayList 這個類別容器第一個元素索引是從 0 開始,所以 arrayA 的索引值要從 0 到 29
REM 我們之前取30個中獎號碼,所以arrayA.Count = 30,再減掉1就是29了
For i As Integer = 0 To arrayA.Count - 1
REM 把30個抽出的號碼放到TextBox1中
TextBox1.Text += CStr(arrayA.Item(i)) + vbCrLf
REM 再把前10個號碼分別放到 TextBox2 和 arrayB 中(這就是第一輪的中獎號碼)
If i < 10 Then
TextBox2.Text += CStr(arrayA.Item(i)) + vbCrLf
arrayB.Add(arrayA.Item(i))
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
REM <<<<<第二輪抽獎程式>>>>>
If arrayB.Count = 0 Then
REM 如果沒有第一輪抽獎,則第二輪抽獎無效
Return
End If
REM 先清空容器
arrayC.Clear()
REM 不要忘記 System.Collections.ArrayList 這個類別容器第一個元素索引是從 0 開始
REM 所以要從 0 到 arrayB.Count - 1 來取得 arrayB 索引值
REM 下面這一行程式就是從第一輪中獎號碼中再抽出3個第二輪中獎的序號
arrayC = MakeRand(0, arrayB.Count - 1, 3)
REM 清空文字框
TextBox3.Clear()
REM 把第二輪中獎的3個號碼放到 TextBox3 中
For i As Integer = 0 To arrayC.Count - 1
REM 利用剛剛抽出的 3 個序號從 arrayB 中取出 3 個號碼並放到 TextBox3 中
TextBox3.Text += CStr(arrayB.Item(CInt(arrayC.Item(i)))) + vbCrLf
Next
End Sub
End Class
複製代碼
我個人建議應該把 "亂數" 這件事情單獨來處理
所以我參考網路上的範例改寫一個MakeRand函數
這個函數我不做註解
希望你可以自行參閱並且去了解它
歡迎光臨 伊莉討論區 (http://a401.file-static.com/)
Powered by Discuz!