大家好,大家好,我想请求你们有关我的 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/