在本教學課程的第四個部分中,您將撰寫 CheckTheAnswer() 這個方法,用於判斷數學問題的答案是否正確。這個主題是有關基本程式碼撰寫概念的教學課程系列的一部分。如需教學課程的概觀,請參閱教學課程 2:建立計時的數學測驗。
注意事項 |
|---|
如果您是在 Visual Basic 中依照步驟一路做下來,您將使用 Function 關鍵字,而不會使用慣用的 Sub 關鍵字,因為這個方法會傳回值。理由就是這麼簡單:Sub 不會傳回值,但 Function 會傳回值。 |
若要驗證答案是否正確
加入 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() 方法,藉此檢查答案。
將下列程式碼加入至 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 Subprivate 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。事件處理常式會停止計時器,顯示恭喜訊息,然後再次啟用 [開始] 按鈕。否則,測驗會繼續。
儲存您的程式,執行程式,開始進行測驗,並提供正確答案給加法問題。
注意事項當您輸入答案時,必須在開始輸入答案之前先選取預設值,或是手動刪除零。您將在本教學課程稍後更正這種行為。
當您提供正確答案時,訊息方塊隨即開啟,[開始] 按鈕會變成可用,且計時器會停止。
若要繼續或檢視
若要移到下一個教學課程步驟,請參閱步驟 5:加入 NumericUpDown 控制項的 Enter 事件處理常式。
若要回到上一個教學課程步驟,請參閱步驟 3:加入倒數計時器。