共用方式為


步驟 6:加入減法問題

在本教學課程的第六個部分中,您將加入減法問題並學習如何執行下列工作:

  • 儲存減法的值。

  • 產生問題的隨機數字 (並確定答案介於 0 和 100 之間)。

  • 更新用來檢查答案的方法,如此該方法就會一併檢查新的減法問題。

  • 更新計時器的 Tick 事件處理常式,讓事件處理常式在時間結束時填入正確答案。

若要加入減法問題

  1. 將減法問題的兩個整數變數加入至表單中,並且放置在加法問題的整數變數與計時器之間。程式碼看起來應該如下所示。

    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 
    
        ' These integer variables store the numbers  
        ' for the subtraction problem.  
        Private minuend As Integer 
        Private subtrahend 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;
    
        // These integer variables store the numbers  
        // for the subtraction problem.  
        int minuend;
        int subtrahend;
    
        // This integer variable keeps track of the  
        // remaining time. 
        int timeLeft;
    

    新的整數變數名稱 minuendsubtrahend 並不是程式設計詞彙。這兩個名稱是算術中的減數 (subtrahend) 和從中減去減數之被減數 (minuend) 的慣用名稱。差等於被減數減去減數。您可以使用其他名稱,因為您程式中的變數、控制項、元件或方法不需要特定名稱。您必須遵循規則 (例如,名稱開頭不可使用數字),但是通常可以使用 x1、x2、x3 或 x4 這類名稱。不過,一般名稱可能使程式碼不易閱讀,而且幾乎難以追蹤問題。為了讓變數名稱保持唯一且實用,稍後在本教學課程中,您將在乘法 (被乘數 × 乘數 = 乘積) 和除法 (被除數 ÷ 除數 = 商數) 中使用慣用名稱。

    接著您將修改 StartTheQuiz() 方法,為減法問題提供隨機值。

  2. 在 "Fill in the subtraction problem" 註解後面加上下列程式碼。

    ''' <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
    
        ' Fill in the subtraction problem.
        minuend = randomizer.Next(1, 101)
        subtrahend = randomizer.Next(1, minuend)
        minusLeftLabel.Text = minuend.ToString()
        minusRightLabel.Text = subtrahend.ToString()
        difference.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;
    
        // Fill in the subtraction problem.
        minuend = randomizer.Next(1, 101);
        subtrahend = randomizer.Next(1, minuend);
        minusLeftLabel.Text = minuend.ToString();
        minusRightLabel.Text = subtrahend.ToString();
        difference.Value = 0;
    
        // Start the timer.
        timeLeft = 30;
        timeLabel.Text = "30 seconds"; 
        timer1.Start();
    }
    

    為避免減法問題得出負數答案,這個程式碼會以稍微不同於加法問題的方式使用 Random 類別的 Next() 方法。當您為 Next() 方法指定兩個值時,它會挑選一個大於或等於第一個值且小於第二個值的隨機數字。下列程式碼會從 1 至 100 選擇一個隨機數字,並將它儲存到被減數變數中。

    minuend = randomizer.Next(1, 101)
    
    minuend = randomizer.Next(1, 101);
    

    您可以透過多種方式呼叫 Random 類別的 Next() 方法,也就是您在本教學課程前段中命名為 "randomizer" 的方法。能夠以多種方式呼叫的方法稱為「多載」(Overload),您可以使用 IntelliSense 來了解這些方法。再看一次 Next() 方法的 [IntelliSense] 視窗工具提示。

    Intellisense 視窗工具提示

    Intellisense 視窗工具提示

    工具提示會顯示 [(+ 2 多載)],也就是說,您可以透過另外兩種方式呼叫 Next() 方法。多載包含不同數目或類型的引數,因此彼此之間的運作方式會稍有不同。例如,某個方法可能會接受單一整數引數,而它的其中一個多載可能會接受一個整數和一個字串。您可以依據需要的功能選擇正確的多載。您將程式碼加入至 StartTheQuiz() 方法時,只要輸入 randomizer.Next(,[IntelliSense] 視窗就會顯示詳細資訊。選擇向上鍵和向下鍵,就可以循環瀏覽這些多載,如下圖所示。

    IntelliSense 中 Next() 方法的多載

    IntelliSense 中 Next() 方法的多載

    在這個案例中,您想要選擇最後一個多載,因為這樣就可以指定最小值和最大值。

  3. 修改 CheckTheAnswer() 方法來檢查正確的減法答案。

    ''' <summary> 
    ''' Check the answers 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 AndAlso 
           minuend - subtrahend = difference.Value Then 
    
            Return True 
        Else 
            Return False 
        End If 
    
    End Function
    
    /// <summary> 
    /// Check the answers 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)
            && (minuend - subtrahend == difference.Value))
            return true;
        else 
            return false;
    }
    

    在 Visual C# 中,&& 是logical and 運算子。在 Visual Basic 中,對等的運算子是 AndAlso。這些運算子表示:「如果 addend1 和 addend2 的總和等於 NumericUpDown 的總和值,以及如果 minuend 減 subtrahend 等於 NumericUpDown 的差值」。只有在加法和減法問題的答案都正確時,CheckTheAnswer() 方法才會傳回 true。

  4. 將計時器之 Tick 事件處理常式的最後一部分取代為下列程式碼,讓事件處理常式在時間結束時填入正確答案。

    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
        difference.Value = minuend - subtrahend
        startButton.Enabled = True 
    End If
    
    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;
        difference.Value = minuend - subtrahend;
        startButton.Enabled = true;
    }
    
  5. 儲存並執行程式碼。

    您的程式包括減法問題,如下圖所示。

    減法問題的數學測驗

    包含減法問題的數學測驗

若要繼續或檢視