数学测验VB 2010奇怪的崩溃

时间:2016-03-29 08:52:32

标签: vb.net math

我在VB 2010上的数学测验存在问题。当我输入错误的数字时,程序崩溃了。谁能帮忙。我编辑了代码,以便没有错误,在视觉上它看起来很好,但我目前有点麻烦。谢谢,这是我的代码

The crash

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If TextBox1.Text = String.Empty Then
        MsgBox("Please type your asnswer :)")
    ElseIf Not IsNumeric(TextBox1.Text) Then
        MsgBox("This is not a valid number.", MsgBoxStyle.Critical)
    End If

    userAnswer = TextBox1.Text

    If userAnswer = numZ Then
        MsgBox("Correct. Proceed to the next question")
        score = score + 1
    Else
        MsgBox("Sorry this answer is incorrect. THe correct answer is " + numZ)
    End If

    Turn2.Text = turn
    turn = turn + 1

    If turn < 10 Then
        numA.Text = (num1.Next(1, 10))
        numB.Text = (num2.Next(1, 10))
        sim = (sim1.Next(1, 4))


        numX = numA.Text
        numY = numB.Text

        Select Case sim
            Case 1
                SimZ.Text = "+"
                numZ = numX + numY
            Case 2
                SimZ.Text = "X"
                numZ = numX * numY
            Case 3
                SimZ.Text = "-"
                numZ = numX - numY
        End Select
    Else
        MessageBox.Show("Your score is " + score + " out of ten")
        Me.Close()
    End If



End Sub

1 个答案:

答案 0 :(得分:2)

+上的

Double和VB.Net中的String尝试将字符串转换为数字。只需在ToString上拨打numZ(并使用&进行连接,因为它不会进行时髦的转换)

错误为clearly documented here