Page 84 -
P. 84
第四章 程式的流程控制
單元練習 4-5
練習一:寫一個程式以亂數產生 6 組 1 到 42 的數字
(一)表單物件說明
物件名稱 屬性 物件樣式
Form1 Text=”樂透彩選號程式”
Text=”樂透彩:”;Font=20pt,粗
Label1 體
AutoSize=False;
TextAlign=MiddleCenter
Label2
Label3 Text=””;Font=20pt,粗體 ;
Label4 AutoSize=False
Label5 TextAlign=MiddleCenter
Label6 BackColor=GreenYellow
Label7
Button Text=”選號”;Font=16pt,粗體
(二)程式碼
Private Sub Button1_Click(sender As System………………………
Dim i, j, num(5) As Integer
Randomize()
For i = 0 To 5
num(i) = Int(Rnd() * 42 + 1)
j=i-1
Do While j >= 0
If num(i) = num(j) Then
num(i) = Int(Rnd() * 42 + 1)
j=j-1
Else
j=j-1
End If
Loop
Next
Array.Sort(num)
Label2.Text = num(0)
Label3.Text = num(1)
Label4.Text = num(2)
Label5.Text = num(3)
Label6.Text = num(4)
Label7.Text = num(5)
End Sub
80