如果在VB.Net上有其他行为,则很奇怪

时间:2014-01-24 07:22:56

标签: vb.net if-statement

大家好日子我们只想请求你关于我的vb.net项目的帮助我的if else语句表现得如此奇怪而不是正确地执行代码当我在文本框中键入正确的答案时它会显示来自if语句的correctmsg表单正确但它也会显示else语句中的错误消息表单。 请帮助我,提前谢谢。这是我的代码:

Private Sub submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit.Click

    If Label1.Text = "Who invented the airplane?" And TextBox1.Text = "third" Then
        Label2.Text = (Label2.Text) + 1

        correctmsg.Show()
        Label1.Text = "Who invented the telephone?"

    Else
        wrongmsg.Show()

    End If

    If Label1.Text = "Who invented the telephone?" And TextBox1.Text = "Alexander Grahambell" Then
        Label2.Text = Label2.Text + 1

        MsgBox("Your Answer is Correct!", MsgBoxStyle.OkOnly)
        Label1.Text = "Who is the first president of the United States of America?"
    Else
        wrongmsg.Show()

    End If



    If Label1.Text = "Who is the first president of the United States of America?" And TextBox1.Text = "George Washington" Then

        Label2.Text = Label2.Text + 1

    Else
        wrongmsg.Show()
    End If




End Sub

4 个答案:

答案 0 :(得分:4)

您错误地识别了哪个 Else块正在运行:

If Label1.Text = "Who invented the airplane?" And TextBox1.Text = "third" Then

   'This block is running

    Label2.Text = (Label2.Text) + 1

    correctmsg.Show()
    Label1.Text = "Who invented the telephone?"

Else
    'This block isn't running

    wrongmsg.Show()

End If

If Label1.Text = "Who invented the telephone?" And TextBox1.Text = "Alexander Grahambell" Then
    'This block isn't running
    Label2.Text = Label2.Text + 1

    MsgBox("Your Answer is Correct!", MsgBoxStyle.OkOnly)
    Label1.Text = "Who is the first president of the United States of America?"
Else
    'THIS Block is running

    wrongmsg.Show()

End If

我会移动代码将问题更改为最后,或者在更改问题后使用Return语句,停止下一组块也检查相同的答案,例如:

If Label1.Text = "Who invented the airplane?" And TextBox1.Text = "third" Then

    Label2.Text = (Label2.Text) + 1

    correctmsg.Show()
    Label1.Text = "Who invented the telephone?"
    Return 'Don't do any more checks this time around

ElseIf Label1.Text = "Who invented the airplane?"
    'Reason ElseIf (In case the question was 'who invented the telephone' then the first errormessage should not not be shown)
    wrongmsg.Show()
    Return

End If

答案 1 :(得分:0)

试试这段代码:

If Label1.Text = "Who invented the airplane?" And TextBox1.Text = "third" Then
    Label2.Text = (Label2.Text) + 1

    correctmsg.Show()
    Label1.Text = "Who invented the telephone?"

ElseIf Label1.Text Is Not "Who invented the airplane?" TextBox1.Text = "third" Then
    wrongmsg.Show()
End If

答案 2 :(得分:0)

在您的代码中,如果第一个if阻止为true,则会将标签Label1.Text更改为"Who invented the telephone?"

在同一个函数中,您创建了另一个if块,用于检查Label1.Text = "Who invented the telephone?"是否为true但是您没有更改文本框中的值,因此{ {1}}条件将失败,并且if块将被执行。

再次在下一个else块中,条件将失败,其if块将被执行。

因此,当您单击按钮一次时,else将至少执行两次。

答案 3 :(得分:0)

我会比其他答案有点差异。通过使用Else If而不是将所有问题分开,如果,您将有机会再调用其他问题。为了好玩,在代码中添加一个断点并逐步执行每一行,您将看到确切的内容。

If Label1.Text = "Who invented the airplane?" Then
    If TextBox1.Text = "third" Then
        Label2.Text = (Label2.Text) + 1

        correctmsg.Show()
        Label1.Text = "Who invented the telephone?"

   Else
        wrongmsg.Show()
    End If
Else If Label1.Text = "Who invented the telephone?" Then
    If TextBox1.Text = "Alexander Grahambell" Then
        Label2.Text = (Label2.Text) + 1

        correctmsg.Show()
        Label1.Text = "Who is the first president of the United States of America?"

   Else
        wrongmsg.Show()
    End If
Else If Label1.Text = "Who is the first president of the United States of America?" Then
    If TextBox1.Text = "George Washington" Then
        Label2.Text = (Label2.Text) + 1

        correctmsg.Show()

   Else
        wrongmsg.Show()
    End If
End If

我也会删除重复的代码。

Dim isCorrect As Boolean = False

If Label1.Text = "Who invented the airplane?" Then
    If TextBox1.Text = "third" Then
        isCorrect = True
        Label1.Text = "Who invented the telephone?"
    End If
Else If Label1.Text = "Who invented the telephone?" Then
    If TextBox1.Text = "Alexander Grahambell" Then
        isCorrect = True
        Label1.Text = "Who is the first president of the United States of America?"
    End If
Else If Label1.Text = "Who is the first president of the United States of America?" Then
    If TextBox1.Text = "George Washington" Then
        isCorrect = True
    End If
End If

If isCorrect Then
    Label2.Text = (Label2.Text) + 1

    correctmsg.Show()
Else
    wrongmsg.Show()
End If