我不知道是否有人會告訴我爲什麼我的代碼不工作。調試顯示Value可以等於1或2,但VB只會執行第一個while語句,而不會執行第二個語句。爲什麼我的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
末級
完成** **任何基本調試,如檢查,如果你的函數實際上是叫什麼名字?檢查'Value'中的值是什麼?而你究竟如何期望你的'Value = 2'代碼運行ince你有一個'Exit Sub'就在它上面? – 2014-09-18 20:39:01
感謝您的回覆。我添加了Exit Sub,因爲我不希望VB在第一個問題得到正確回答後運行腳本的其餘部分。我的希望是,如果發電機產生了2個,那麼第二個問題就會顯示出來。我是VB新手,所以我的調試技巧是有限的,儘管我已經嘗試了Step Into。 – RickTicky 2014-09-18 21:14:24
Step Into顯示Value可以= 1或= 2,但代碼將只運行第一個While響應。即使值爲2,它也不會跳到第二位。我希望您提供任何進一步的建議。 – RickTicky 2014-09-18 21:33:40