Public Class Form1
    Dim ti_shu As Integer, right_shu As Integer, result As Integer
    Private Sub chu_ti()
        Dim t, p, b, a As Integer
        Randomize(TimeOfDay.ToOADate)
        a = Int(10 + 90 * Rnd())
        b = Int(10 + 90 * Rnd())   '产生范围为10~99的两个随机整数
        p = Int(2 * Rnd())        '返回一个[0，1]之间的整数
        Select Case p
            Case 0      ' 出加法题
                Label1.Text = Str(a) & " + " & Str(b) & " = "
                result = a + b
            Case 1      ' 出减法题
                If a < b Then
                    t = a
                    a = b
                    b = t
                End If
                Label1.Text = Str(a) & " - " & Str(b) & " = "
                result = a - b
        End Select
        ti_shu = ti_shu + 1   ' 出题数加1
        TextBox1.Text = ""      ' 清空答题文本框
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ti_shu = 0       ' 出题数清零
        right_shu = 0    ' 答对题数清零
        Call chu_ti()    ' 调用出题过程，出第一道题
    End Sub
    '答题部分由文本框的按键（KeyPress）事件代码完成：
    Private Sub TextBox1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
        Dim Item As String
        If KeyAscii = 13 Then               ' 表示按下的是回车键
            If TextBox1.Text = result Then
                Item = Label1.Text & TextBox1.Text & " √"
                right_shu = right_shu + 1
            Else
                Item = Label1.Text & TextBox1.Text & " ×"
            End If
            ListBox1.Items.Insert(0, Item) ' 将题目和回答插入到列表框中的第一项
            Label2.Text = "共" & ti_shu & "题" & Chr(13) & "正确率为:" & Int(right_shu / ti_shu * 10000) / 100 & "%"
            Call chu_ti()              ' 调用出题过程，出下一道题
        End If
    End Sub
End Class
