为什么我的While循环不能正常工作?

时间:2014-09-18 20:36:20

标签: vb.net if-statement while-loop

我想知道是否有人会如此友善地告诉我为什么我的代码无效。调试显示Value可以等于1或2,但VB只执行第一个while语句,而不是第二个。

Const AnswerA As String = "Copenhagen"
Const AnswerB As String = "Peter the Great"
Dim Value As Integer
Dim Message, Response As String
Private Sub btnAskMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAskMe.Click
    Randomize()
    ' Generate random value between 1 and 2 to test only. 
    Value = CInt(Int((2 * Rnd()) + 1))
    If Value = 1 Then
        Message = "What's the capital of Denmark?"
        While Response <> AnswerA
            Response = InputBox(Message)
            If String.IsNullOrEmpty(Response) Then Exit Sub
            If Response <> AnswerA Then Message = "No, no, no! Think boy, think?"
        End While
        MsgBox("Correct....What took you so long?")
    End If
    If Value = 2 Then
        Message = "Which Russian Tsar forced his wife to keep her lovers head in a jar?"
        While Response <> AnswerB
            Response = InputBox(Message)
            If String.IsNullOrEmpty(Response) Then Exit Sub
            If Response <> AnswerB Then Message = "No you twerp! Have another go"
        End While
        MsgBox("Correct....What took you so long?")
    End If
End Sub

结束班

2 个答案:

答案 0 :(得分:1)

你的“end if”位置有问题 这里

    If Value = 1 Then
        Message = "What's the capital of Denmark?"
    End If

在这里

    If Value = 2 Then
        Message = "Which Russian Tsar forced his wife to keep her lovers head in a jar?"
    End If

如果需要后来的第一个结束 “退出子”行 然后是第二个 “结束时”

答案 1 :(得分:0)

它永远不会正常工作:

    While ResponseB <> AnswerB
                  ^----check Response **B**
        ResponseA = InputBox(Message)
                ^---- get a response into Response **A**