共用方式為


步驟 4:加入 CheckTheAnswer() 方法

您的測驗需要驗證使用者是否回答正確。幸好,撰寫方法來執行簡單的計算 (例如 CheckTheAnswer() 方法) 並不困難。

注意事項注意事項

如果您是在 Visual Basic 中依照步驟一路做下來,請注意,因為此方法會傳回一個值,您將使用 Function 關鍵字,而不會使用慣用的 Sub 關鍵字。理由就是這麼簡單:Sub 不會傳回值,但 Function 會傳回值。

若要加入 CheckTheAnswer() 方法

  1. 加入 CheckTheAnswer() 方法,此方法會加入 addend1 和 addend2,並驗證總和是否等於總和 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 = 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 控制項出現異常的情形。如果您沒有選取整個答案就開始輸入,則零會遺留下來,而您必須手動刪除它。您在本教學課程稍後將會更正這種情形。

  4. 當您輸入正確答案時,訊息方塊應該開啟、[開始] 按鈕應該可用,且計時器應該停止。再按一次 [開始] 按鈕,並確定這些行為都很正常。

若要繼續或檢視