Public Class Form1
    Dim n(3), user_n(3) As Short
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
20:     Dim i As Short
30:     Randomize()     '設定亂數的序列
40:     For i = 0 To 3  '隨機產生4個介於0∼9的數字
50:         n(i) = Int(Rnd() * 10)
60:     Next
    End Sub

    Private Sub TextBoxes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
        '利用TabIndex屬性設定值將使用者在文字方塊中所輸入的數字，存至陣列user_n( )中
70:     Select Case sender.TabIndex
            Case 0 : user_n(0) = Int(TextBox1.Text)
            Case 1 : user_n(1) = Int(TextBox2.Text)
            Case 2 : user_n(2) = Int(TextBox3.Text)
            Case 3 : user_n(3) = Int(TextBox4.Text)
80:             End Select
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
90:     Dim k, count As Short
100:    count = 0
110:    For k = 0 To 3      '計算使用者猜中幾個數字，並顯示電腦隨機產生的4個數字
120:        If user_n(k) = n(0) Or user_n(k) = n(1) Or user_n(k) = n(2) Or user_n(k) = n(3) Then count = count + 1
130:        Label2.Text &= n(k) & Space(2)
140:    Next
150:    If count > 0 Then   '判斷使用者是否有猜中數字
160:        Label3.Text = "猜中 " & count & " 個數字"
170:    Else
180:        Label3.Text = "很抱歉，沒有猜中任何數字！"
190:    End If
    End Sub
End Class
