如何让我的代码在Visual Basic中停止?

时间:2013-07-09 10:03:30

标签: arrays vb.net inputbox

我正在创建一个游戏,其中玩家回答问题,然后如果他们正确回答,那么他们可以翻转两张牌以查看它们是否匹配(有点像对子游戏)。但是,我正在努力弄清楚我如何能够停止输入框(用户如何输入他们的答案),如果玩家得到正确的问题,从出现一段时间直到玩家选择了两张牌。

我使用两个数组,一个用于保存问题,另一个用于保存答案。附:请原谅最后几个问题,在我完成这些问题之前尝试让它工作。

Private Sub Questions()

    strQuestions(0) = "A common noun refers to the name of things."
    strQuestions(1) = "A pronoun is a word that takes the place of nouns."
    strQuestions(2) = "'She' is a pronoun that would replace a woman's name."
    strQuestions(3) = "The days of the week are proper nouns."
    strQuestions(4) = "'He', 'She' and 'them' are all pronouns."
    strQuestions(5) = "Spain is a proper noun."
    strQuestions(6) = "Proper nouns always start with a capital letter."
    strQuestions(7) = "The place 'England' is referred to as a proper noun."
    strQuestions(8) = "A 'camera' is a common noun."
    strQuestions(9) = "FE"
    strQuestions(10) = "GR"

    strAnswers(0) = "TRUE"
    strAnswers(1) = "TRUE"
    strAnswers(2) = "TRUE"
    strAnswers(3) = "TRUE"
    strAnswers(4) = "TRUE"
    strAnswers(5) = "TRUE"
    strAnswers(6) = "TRUE"
    strAnswers(7) = "TRUE"
    strAnswers(8) = "TRUE"
    strAnswers(9) = "TRUE"
    strAnswers(10) = "TRUE"

    Dim userAnswer As String

    For i = 0 To UBound(strQuestions)
        userAnswer = InputBox(strQuestions(i))
        If userAnswer <> strAnswers(i) Then
            MsgBox("Incorrect. Try again.")
        Else
            MsgBox("Correct! Make your move.")
            WHAT GOES HERE?
        End If
    Next i
End Sub

Private Sub label_click(ByVal sender As System.Object,
                        ByVal e As System.EventArgs) Handles Label9.Click, Label8.Click, Label7.Click, Label6.Click, Label5.Click,
                    Label4.Click, Label3.Click, Label2.Click, Label16.Click, Label15.Click, Label14.Click, Label13.Click, Label12.Click,
                    Label11.Click, Label10.Click, Label1.Click

    ' The timer is only on after two non-matching 
    ' icons have been shown to the player, 
    ' so ignore any clicks if the timer is running
    If Timer1.Enabled Then Exit Sub

    Dim clickedLabel = TryCast(sender, Label)

    If clickedLabel IsNot Nothing Then

    End If
    ' If the clicked label is black, the player clicked
    ' an icon that's already been revealed --
    ' ignore the click
    If clickedLabel.ForeColor = Color.Black Then Exit Sub

    ' If firstClicked is Nothing, this is the first icon 
    ' in the pair that the player clicked, 
    ' so set firstClicked to the label that the player 
    ' clicked, change its color to black, and return
    If firstClicked Is Nothing Then
        firstClicked = clickedLabel
        firstClicked.ForeColor = Color.Black
        Exit Sub
    End If

    ' If the player gets this far, the timer isn't 
    ' running and firstClicked isn't Nothing, 
    ' so this must be the second icon the player clicked
    ' Set its color to black
    secondClicked = clickedLabel
    secondClicked.ForeColor = Color.Black

    CheckForWinner()

    ' If the player clicked two matching icons, keep them 
    ' black and reset firstClicked and secondClicked 
    ' so the player can click another icon
    If firstClicked.Text = secondClicked.Text Then
        firstClicked = Nothing
        secondClicked = Nothing
        Questions()
        Exit Sub
    End If

    ' If the player gets this far, the player 
    ' clicked two different icons, so start the 
    ' timer (which will wait three quarters of 
    ' a second, and then hide the icons)
    Timer1.Start()

    Questions()
Exit Sub

1 个答案:

答案 0 :(得分:1)

您需要重新构建程序,以便询问问题的部分不在循环中。而是考虑使用一个要求下一个答案的子。如果给出了正确答案,则子退出并且应用程序等待用户选择卡。

选择卡片后,再次拨打问题。通过问题列表的位置存储为表单中的变量。每次执行询问子问题时,它会检查是否已到达列表的末尾,如果是,则结束游戏。

像这样的东西(虽然我写了任何VB已经有一段时间了!)

Private CurrentQuestion As Integer

Private Sub Questions()
    CurrentQuestion = 0

    ...


    AskQuestion
End Sub

Private Sub AskQuestion
    If CurrentQuestion = UBound(strQuestions)
        EndGame
    Else
        userAnswer = InputBox(strQuestions(CurrentQuestion))
        If userAnswer <> strAnswers(CurrentQuestion) Then
            MsgBox("Incorrect. Try again.")
            AskQuestion
        Else
            CurrentQuestion = CurrentQuestion + 1
            MsgBox("Correct! Make your move.")
        End If
    End If
End Sub

Private Sub label_click(ByVal sender As System.Object,

    ....

    AskQuestion
End Sub