步骤 4:添加 CheckTheAnswer() 方法

测验需要验证用户的答案是否正确。幸运的是,编写执行简单计算的方法(如 CheckTheAnswer() 方法)并不难。

说明说明

对于使用 Visual Basic 的用户而言,有一点值得注意,由于此方法返回一个值,因此您将使用 Function 关键字来代替常见的 Sub 关键字。真的很简单:Sub 不会返回值,但函数会返回值。

添加 CheckTheAnswer() 方法

  1. 添加 CheckTheAnswer() 方法,该方法将 addend1 和 addend2 相加,并验证得到的和与 sum NumericUpDown 控件中的值是否相等。如果和相等,则此方法返回 true;否则返回 false。您的代码应类似以下内容。

    ''' <summary>
    ''' Check the answer to see if the user got everything right.
    ''' </summary>
    ''' <returns>True if the answer's correct, false otherwise.</returns>
    ''' <remarks></remarks>
    Public Function CheckTheAnswer() As Boolean
    
        If addend1 + addend2 = sum.Value Then
            Return True
        Else
            Return False
        End If
    
    End Function
    
    /// <summary>
    /// Check the answer to see if the user got everything right.
    /// </summary>
    /// <returns>True if the answer's correct, false otherwise.</returns>
    private bool CheckTheAnswer()
    {
        if (addend1 + addend2 == sum.Value)
            return true;
        else
            return false;
    }
    

    程序需要调用此方法以验证用户的答案是否正确。可以通过添加 if else 语句来做到这一点。该语句看起来类似下面这样。

    If CheckTheAnswer() Then
        ' statements that will get executed
        ' if the answer is correct 
    ElseIf timeLeft > 0 Then
        ' statements that will get executed
        ' if there's still time left on the timer
    Else
        ' statements that will get executed if the timer ran out
    End If
    
    if (CheckTheAnswer())
    {
          // statements that will get executed
          // if the answer is correct 
    }
    else if (timeLeft > 0)
    {
          // statements that will get executed
          // if there's still time left on the timer
    }
    else
    {
          // statements that will get executed if the timer ran out
    }  
    
  2. 接下来,修改计时器的 Tick 事件处理程序以检查答案。带有答案检查功能的新事件处理程序应包括以下内容。

    Private Sub Timer1_Tick() Handles Timer1.Tick
    
        If CheckTheAnswer() Then
            ' If the user got the answer right, stop the timer
            ' and show a MessageBox.
            Timer1.Stop()
            MessageBox.Show("You got all of the answers right!", "Congratulations!")
            startButton.Enabled = True
        ElseIf timeLeft > 0 Then
            ' Decrease the time left by one second and display
            ' the new time left by updating the Time Left label.
            timeLeft -= 1
            timeLabel.Text = timeLeft & " seconds"
        Else
            ' If the user ran out of time, stop the timer, show
            ' a MessageBox, and fill in the answers.
            Timer1.Stop()
            timeLabel.Text = "Time's up!"
            MessageBox.Show("You didn't finish in time.", "Sorry")
            sum.Value = addend1 + addend2
            startButton.Enabled = True
        End If
    
    End Sub
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (CheckTheAnswer())
        {
            // If the user got the answer right, stop the timer 
            // and show a MessageBox.
            timer1.Stop();
            MessageBox.Show("You got all the answers right!",
                            "Congratulations");
            startButton.Enabled = true;
        }
        else if (timeLeft > 0)
        {
            // Decrease the time left by one second and display 
            // the new time left by updating the Time Left label.
            timeLeft--;
            timeLabel.Text = timeLeft + " seconds";
        }
        else
        {
            // If the user ran out of time, stop the timer, show
            // a MessageBox, and fill in the answers.
            timer1.Stop();
            timeLabel.Text = "Time's up!";
            MessageBox.Show("You didn't finish in time.", "Sorry");
            sum.Value = addend1 + addend2;
            startButton.Enabled = true;
        }
    }
    

    此时,如果计时器的事件处理程序确认用户的答案正确,则该事件处理程序将停止计时器并显示祝贺消息,然后使**“开始”**按钮重新可用。

  3. 保存并运行程序。开始游戏,然后键入加法问题的正确答案。

    说明说明

    在键入答案时,您可能会注意到有关 NumericUpDown 控件的意外情况。如果在未选择整个答案的情况下开始键入,则原来的 0 将会保留,并且您必须手动将其删除。本教程后面将更正此情况。

  4. 当您键入正确答案之后,消息框将打开,**“开始”按钮将可用,并且计时器将停止。再次单击“开始”**按钮以确保此情况属实。

继续或查看