将 TextBox 文本值与整数进行比较不起作用

Comparing TextBox Text value to integers doesn't work

让我布景: 我正在为学校创建一个时间 table 的练习测验,需要将一个变量与 TextBox1.Text 的值进行比较。变量类型为 Integer

这是我的代码:

    Dim answer As Integer = (num1 * num2)

    If TextBox1.Text = answer.ToString Then
        question()
    Else
        Dim typedAnswer As String = TextBox1.Text
        timeCheck.Stop()
        MsgBox("Sorry, " & typedAnswer & " is not the correct answer. Please try again.")
        timeCheck.Start()
    End If

澄清一下:

question 是我创建的子

answer 是问题的答案 Integer

num1 是被乘数之一

num2是被乘的另一个数

每当我按下触发此代码的按钮时,它只会激活表示答案不正确的代码,即使它是正确的。例如:我得到问题 9 multiplied by 5。然后我会输入 45,但代码会激活代码的底部。

有什么想法吗?

尝试比较喜欢

If val(TextBox1.Text) = answer Then
        question()
    Else
        Dim typedAnswer As String = TextBox1.Text
        timeCheck.Stop()
        MsgBox("Sorry, " & typedAnswer & " is not the correct answer. Please try again.")
        timeCheck.Start()
    End If

将数字值用作字符串并不好practice.hope它有帮助。

以下代码可能对您有所帮助:

If CInt(TextBox1.Text) = answer Then
    ...
End If

那应该没问题。

找到原因了。我每次都调用 num1num2,它们的值为 rnd.Next(1, 12)rnd 等于 New Random(),这意味着它每次都会生成新数字!对于那个很抱歉!菜鸟失误!