vb.net - VB.Net 上奇怪的 If Else 行为

标签 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

最佳答案

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

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 语句,以停止下一组 block 也检查相同的答案,例如:

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

关于vb.net - VB.Net 上奇怪的 If Else 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21326766/

相关文章:

WPF 可以禁用窗口中的所有点击事件

c# - 如何将图像 url 转换为 system.drawing.image

arrays - 如何仅在搜索栏文本不为空时才运行过滤功能?

c++ - If 语句和大括号..有/没有不同的结果

python - 循环语句else子句的启示和影响?

VB.NET 和 Linq : Sum datatable column and Group by date in another column

vb.net - 在VB.NET中评估IF语句

vb.net - 使用 FtpWebRequest 将文件从 azure 托管应用程序上传到外部 ftp 站点

c - 警告 : statement with no effect [-Wunused-value] with if statements

c++ - ',' 在 if 语法中意味着什么?