Step 4: Add the CheckTheAnswer() Method

在本教程的第 4 部分中,您将编写一个方法 CheckTheAnswer(),用于判定数学题的答案是否正确。本主题是基本编码概念教程系列中的一部分。有关本教程的概述,请参阅Tutorial 2: Create a Timed Math Quiz

说明说明

如果您一直使用 Visual Basic 学习本教程,则将使用 Function 关键字而不是一般的 Sub 关键字,因为此方法将返回一个值。真的很简单:sub 不会返回值,但函数会返回值。

验证答案是否正确

  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;
    }
    

    接下来,您将通过更新计时器 Tick 事件处理程序方法中的代码来调用新的 CheckTheAnswer() 方法检查答案。

  2. 将下面的代码添加到 if else 语句。

    Private Sub Timer1_Tick() Handles Timer1.Tick
    
        If CheckTheAnswer() Then 
            ' If CheckTheAnswer() returns true, then 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 
            ' If CheckTheAnswer() return false, keep counting 
            ' down. 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 CheckTheAnswer() returns true, then 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)
        {
           // If CheckTheAnswer() return false, keep counting 
           // down. 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;
        }
    }
    

    如果答案正确,CheckTheAnswer() 将返回 true。事件处理程序将停止计时器,并显示祝贺消息,然后使**“开始”**按钮再次可用。否则,继续进行测验。

  3. 保存并运行您的程序,开始测验,并提供加法题的正确答案。

    说明说明

    在输入答案时,您必须在开始输入答案前选择默认值或手动删除零。您将在本教程的后面部分中纠正这一行为。

    提供正确答案后,将打开一个消息框,**“开始”**按钮将变得可用,同时计时器将停止。

继续或查看