共用方式為


步驟 3:加入倒數計時器

在本教學課程的第三個部分中,您將加入倒數計時器來追蹤受測者可完成作答的剩餘秒數。

注意事項注意事項

這個主題是有關基本程式碼撰寫概念的教學課程系列的一部分。如需教學課程的概觀,請參閱教學課程 2:建立計時的數學測驗

若要加入倒數計時器

  1. 加入名為 timeLeft 的整數變數,就像在前一個程序中所進行。您的程式碼應該看起來與下列範例相同。

    Public Class Form1
    
        ' Create a Random object called randomizer  
        ' to generate random numbers. 
        Private randomizer As New Random
    
        ' These integer variables store the numbers  
        ' for the addition problem.  
        Private addend1 As Integer 
        Private addend2 As Integer 
    
        ' This integer variable keeps track of the  
        ' remaining time. 
        Private timeLeft As Integer
    
    public partial class Form1 : Form
    {
        // Create a Random object called randomizer  
        // to generate random numbers.
        Random randomizer = new Random();
    
        // These integer variables store the numbers  
        // for the addition problem.  
        int addend1;
        int addend2;
    
        // This integer variable keeps track of the  
        // remaining time. 
        int timeLeft;
    

    現在您需要實際計算秒數的方法,例如計時器,它會在經過您指定的時間後引發事件。

  2. 在設計視窗中,從 [工具箱] 的 [元件] 類別將 Timer 控制項移至表單。

    控制項會出現在設計視窗底部的灰色區域中。

  3. 在表單上選擇您剛加入的 [timer1] 圖示,將其 [Interval] 屬性設定為 [1000]。

    由於間隔值為毫秒,因此 1000 這個值會讓 Tick 事件每秒引發一次。

  4. 在表單上按兩下 [Timer] 控制項,或選擇該控制項,然後選擇 Enter 鍵。

    程式碼編輯器隨即出現,並且顯示您剛才為 Tick 事件處理常式加入的方法。

  5. 將下列陳述式加入至新的事件處理常式方法。

    Private Sub Timer1_Tick() Handles Timer1.Tick
    
        If timeLeft > 0 Then 
            ' 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 (timeLeft > 0)
        {
            // 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;
        }
    }
    

    根據您加入的項目,計時器會藉由判斷 timeLeft 整數變數是否大於 0,每秒檢查一次時間是否已結束。如果是,表示仍有剩餘時間。計時器會先將 timeLeft 減 1,然後更新 timeLabel 控制項的 [Text] 屬性,讓受測者看到還剩多少秒。

    如果沒有剩餘時間,計時器就會停止,並將 timeLabel 控制項的文字變更為顯示 [Time's up!]。此時會出現訊息方塊宣布測驗結束,並且公布答案,這個案例為 addend1 和 addend2 相加。startButton 控制項的 [Enabled] 屬性會設為 true,如此受測者就能開始另一項測驗。

    您已加入 if else 陳述式,讓程式知道如何做判斷。if else 陳述式看起來如下。

    注意事項注意事項

    下列範例僅供參考,請不要將它加入您的專案中。

    If (something that your program will check) Then
        ' One or more statements that will run
        ' if what the program checked is true. 
    Else
        ' One or more statements that will run
        ' if what the program checked is false.
    End If
    
    if (something that your program will check)
    {
        // One or more statements that will run
        // if what the program checked is true. 
    }
    else
    {
        // One or more statements that will run
        // if what the program checked is false.
    }
    

    仔細查看您在 else 區塊中加入,用來顯示加法問題答案的陳述式。

    sum.Value = addend1 + addend2
    
    sum.Value = addend1 + addend2;
    

    addend1 + addend2 陳述式會將兩個變數中的值相加。第一個部分 (sum.Value) 會使用 sum (總和) NumericUpDown 控制項的 [Value] 屬性來顯示正確答案。稍後您會使用相同屬性檢查測驗的答案。

    受測者透過使用 NumericUpDown 控制項就能更輕鬆地輸入數字,這就是為何使用其中一個控制項輸入數學問題答案的原因。所有可能的答案包括從 0 到 100 的整數。藉由保留 [Minimum]、[Maximum] 和 [DecimalPlaces] 屬性的預設值,就能確保受測者無法輸入小數、負數或太大的數字 (如果您要允許受測者輸入 3.141 而不是 3.1415,可以將 [DecimalPlaces] 屬性設定為 3)。

  6. 將三行程式碼加入至 StartTheQuiz() 方法的結尾,使程式碼看起來如下。

    ''' <summary> 
    ''' Start the quiz by filling in all of the problem  
    ''' values and starting the timer.  
    ''' </summary> 
    ''' <remarks></remarks> 
    Public Sub StartTheQuiz()
    
        ' Fill in the addition problem. 
        ' Generate two random numbers to add. 
        ' Store the values in the variables 'addend1' and 'addend2'.
        addend1 = randomizer.Next(51)
        addend2 = randomizer.Next(51)
    
        ' Convert the two randomly generated numbers 
        ' into strings so that they can be displayed 
        ' in the label controls.
        plusLeftLabel.Text = addend1.ToString()
        plusRightLabel.Text = addend2.ToString()
    
        ' 'sum' is the name of the NumericUpDown control. 
        ' This step makes sure its value is zero before 
        ' adding any values to it.
        sum.Value = 0
    
        ' Start the timer.
        timeLeft = 30
        timeLabel.Text = "30 seconds"
        Timer1.Start()
    
    End Sub
    
    /// <summary> 
    /// Start the quiz by filling in all of the problem  
    /// values and starting the timer.  
    /// </summary> 
    public void StartTheQuiz()
    {
        // Fill in the addition problem. 
        // Generate two random numbers to add. 
        // Store the values in the variables 'addend1' and 'addend2'.
        addend1 = randomizer.Next(51);
        addend2 = randomizer.Next(51);
    
        // Convert the two randomly generated numbers 
        // into strings so that they can be displayed 
        // in the label controls.
        plusLeftLabel.Text = addend1.ToString();
        plusRightLabel.Text = addend2.ToString();
    
    
        // 'sum' is the name of the NumericUpDown control. 
        // This step makes sure its value is zero before 
        // adding any values to it.
        sum.Value = 0;
    
        // Start the timer.
        timeLeft = 30;
        timeLabel.Text = "30 seconds"; 
        timer1.Start();
    }
    

    現在,當測驗開始時,[timeLeft] 變數會設定為 30,而且 timeLabel 控制項的 [Text] 屬性會設定為 30 秒。然後,Timer 控制項的 Start() 方法就會開始倒數計時 (測驗還不會檢查答案,這是下一個部分)。

  7. 儲存您的程式,執行程式,然後選擇表單上的 [開始] 按鈕。

    計時器就會開始倒數。當時間結束時,測驗就會結束,答案也會出現。下圖將顯示進行中的測驗。

    進行中的數學測驗

    數學測驗正進行中

若要繼續或檢視