VB.NET测验中哪里发生System.StackOverflowException?

时间:2019-02-19 11:09:03

标签: vb.net stack-overflow

enter image description here

该错误似乎发生在Dim totalAdd, totalSub, totalDivtotalMult中,但这很奇怪,因为它之前就可以工作,并且看不到哪里可能出错了。

Public Class QuizForm
    Public Property Sign As String
    Dim Rnd1 As New Random
    Dim Rnd2 As New Random
    Dim Num1 = Rnd1.Next(1, 10)
    Dim Num2 = Rnd2.Next(1, 10)
    Dim totalAdd = Num1 + Num2
    Dim totalSub = Num1 - Num2
    Dim totalDiv = Num1 / Num2
    Dim totalMult = Num1 * Num2
    Dim Score As Integer
    Dim Questions As Integer
    Dim prompt As String
    Private Sub QuizForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = Num1
        TextBox2.Text = Num2
        signLabel.Text = Sign

        If Sign = "+" Then
            answerLabel.Text = totalAdd
        ElseIf Sign = "-" Then
            answerLabel.Text = totalSub
        ElseIf Sign = "÷" Then
            answerLabel.Text = totalDiv
        Else
            answerLabel.Text = totalMult
        End If
    End Sub

    Private Sub Checker_Click(sender As Object, e As EventArgs) Handles Checker.Click
        If Sign = "+" Then
            If answerBox.Text = answerLabel.Text Then
                prompt = "Correct"
                Score += 1
                answerBox.Text = ""
            Else
                prompt = "Incorrect"
                answerBox.Text = ""
            End If


            Dim confirm As Integer = MessageBox.Show(prompt, "Answer", MessageBoxButtons.OK)
            If confirm = DialogResult.OK Then
                Num1 = Rnd1.Next(1, 10)
                Num2 = Rnd2.Next(1, 15)

                totalAdd = Num1 + Num2
                TextBox1.Text = Num1
                TextBox2.Text = Num2
                answerLabel.Text = totalAdd
                scoreLabel.Text = "Score: " & Score
            End If

            Questions += 1
            questionNum.Text = ("Question Number " & Questions & "/10")


        ElseIf Sign = "-" Then
            If answerBox.Text = answerLabel.Text Then
                prompt = "Correct"
                Score += 1
                answerBox.Text = ""
            Else
                prompt = "Incorrect"
                answerBox.Text = ""
            End If


            Dim confirm As Integer = MessageBox.Show(prompt, "Answer", MessageBoxButtons.OK)
            If confirm = DialogResult.OK Then
                Num1 = Rnd1.Next(1, 15)
                Num2 = Rnd2.Next(1, 10)

                totalSub = Num1 - Num2
                TextBox1.Text = Num1
                TextBox2.Text = Num2
                answerLabel.Text = totalSub
                scoreLabel.Text = "Score: " & Score
            End If

            Questions += 1
            questionNum.Text = ("Question Number " & Questions & "/10")

        ElseIf Sign = "÷" Then
            If answerBox.Text = answerLabel.Text Then
                prompt = "Correct"
                Score += 1
                answerBox.Text = ""
            Else
                prompt = "Incorrect"
                answerBox.Text = ""
            End If


            Dim confirm As Integer = MessageBox.Show(prompt, "Answer", MessageBoxButtons.OK)
            If confirm = DialogResult.OK Then
                Num1 = Rnd1.Next(1, 10)
                Num2 = 2

                totalDiv = Num1 / Num2
                TextBox1.Text = Num1
                TextBox2.Text = Num2
                answerLabel.Text = totalDiv
                scoreLabel.Text = "Score: " & Score
            End If

            Questions += 1
            questionNum.Text = ("Question Number " & Questions & "/10")

        Else
            If answerBox.Text = answerLabel.Text Then
                prompt = "Correct"
                Score += 1
                answerBox.Text = ""
            Else
                prompt = "Incorrect"
                answerBox.Text = ""
            End If


            Dim confirm As Integer = MessageBox.Show(prompt, "Answer", MessageBoxButtons.OK)
            If confirm = DialogResult.OK Then
                Num1 = Rnd1.Next(1, 10)
                Num2 = Rnd1.Next(1, 10)

                totalMult = Num1 * Num2
                TextBox1.Text = Num1
                TextBox2.Text = Num2
                answerLabel.Text = totalMult
                scoreLabel.Text = "Score: " & Score
            End If

            Questions += 1
            questionNum.Text = ("Question Number " & Questions & "/10")
        End If
    End Sub

    Private Sub menuButton_Click(sender As Object, e As EventArgs) Handles menuButton.Click
        Dim quiz_menu As New MenuForm
        quiz_menu.Show()
        Me.Hide()
    End Sub
End Class

0 个答案:

没有答案