教學課程: 將數學問題新增至數學測驗 WinForms 應用程式

在此系列的四個教學課程中,您將建置數學測驗。 測驗包含四個隨機的數學問題,讓測驗者嘗試在指定的時間內回答。

控制項會使用 C# 或 Visual Basic 程式碼。 在第二個教學課程中,您會為以隨機數字為基礎的數學問題新增程式碼,讓測驗更具挑戰性。 您也會建立名為 StartTheQuiz() 的方法,以填入問題。

在第二個教學課程中,您將了解如何:

  • 撰寫程式碼以建立隨機物件以用於數學問題。
  • 新增啟動按鈕的事件處理常式。
  • 撰寫程式碼以開始測驗。

必要條件

本教學課程是以上一個教學課程 (建立數學測驗 WinForms 應用程式) 為基礎。 如果您尚未完成該教學課程,請先完成它。

建立隨機加法問題

  1. 在 Visual Studio 專案中,選取 [Windows Forms 設計工具]

  2. 選取表單,Form1

  3. 在功能表列中,選取 [檢視]>[程式碼]。 依您使用的程式設計語言而定,會出現 Form1.csForm1.vb,如此您就可以檢視表單的後置程式碼。

  4. 在靠近程式碼頂端的位置新增 new 陳述式以建立 Random 物件。

    public partial class Form1 : Form
    {
        // Create a Random object called randomizer 
        // to generate random numbers.
        Random randomizer = new Random();
    

重要

使用此頁面右上方的程式設計語言控制項來檢視 C# 程式碼片段或 Visual Basic 程式碼片段。

Programming language control for Microsoft Learn

您可以使用類似這個的 new 陳述式建立按鈕、標籤、面板、OpenFileDialogs、ColorDialogs、SoundPlayers、Randoms,甚至表單。 這些項目稱為 物件

當您執行程式時,會啟動表單。 其後置的程式碼會建立一個隨機物件,並將它命名為 隨機化程式

您的測驗需要變數來儲存針對每個問題建立的隨機數字。 正確使用變數前,您必須宣告變數,這表示將它們的名稱和資料類型列出。

  1. 將兩個整數變數加入至表單,並命名為 addend1addend2

    注意

    整數變數在 C# 中稱為 [int],在 Visual Basic 中則稱為 [Integer]。 這類變數會儲存從 -2147483648 到 2147483647 的正數或負數,但是只能儲存整數,不包括小數。

    您可以使用類似的語法來新增一個整數變數,就像您新增隨機物件一般,如下列程式碼所示。

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

  1. 新增名為 StartTheQuiz() 的方法。 這個方法會使用隨機物件的 Next() 方法來產生標籤的隨機數字。 StartTheQuiz() 最後會填入所有問題,然後啟動計時器,所以請將此資訊加入摘要註解中。 函式看起來應該如下列程式碼。

    /// <summary>
    /// Start the quiz by filling in all of the problems
    /// 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;
    }
    

當您使用 Next() 方法搭配隨機物件時 (例如,當您呼叫 randomizer.Next(51) 時),您會得到小於 51 (從 0 到 50) 的隨機數字。 此程式碼會呼叫 randomizer.Next(51),讓兩個隨機數字加總到介於 0 到 100 之間的答案。

仔細查看這些陳述式。

plusLeftLabel.Text = addend1.ToString();
plusRightLabel.Text = addend2.ToString();

這些陳述式會設定 [plusLeftLabel] 和  [plusRightLabel][文字] 屬性,讓它們顯示兩個隨機數字。 標籤控制項會以文字格式顯示值,在程式設計中,則是字串保留文字。 每個整數的 ToString() 方法會將整數轉換成標籤可以顯示的文字。

建立隨機減法、乘法和除法問題

下一個步驟是宣告變數,並為其他數學問題提供隨機值。

  1. 在加法問題變數之後,將剩餘數學問題的整數變數新增至表單。 程式碼看起來應該如下列樣本所示。

    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;
    
        // These integer variables store the numbers 
        // for the multiplication problem. 
        int multiplicand;
        int multiplier;
    
        // These integer variables store the numbers 
        // for the division problem. 
        int dividend;
        int divisor;
    

  1. 藉由新增下列程式碼,從「填入減法問題」差集開始,修改 StartTheQuiz() 方法。

    /// <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;
    
        // Fill in the multiplication problem.
        multiplicand = randomizer.Next(2, 11);
        multiplier = randomizer.Next(2, 11);
        timesLeftLabel.Text = multiplicand.ToString();
        timesRightLabel.Text = multiplier.ToString();
        product.Value = 0;
    
        // Fill in the division problem.
        divisor = randomizer.Next(2, 11);
        int temporaryQuotient = randomizer.Next(2, 11);
        dividend = divisor * temporaryQuotient;
        dividedLeftLabel.Text = dividend.ToString();
        dividedRightLabel.Text = divisor.ToString();
        quotient.Value = 0;
    

這個程式碼會使用 Random 類別的 Next() 方法,與加法問題的方式稍微不同。 當您為 Next() 方法指定兩個值時,它會挑選一個大於或等於第一個值且小於第二個值的隨機數字。

藉由搭配兩個引數使用 Next() 方法,您可以確保減法問題的答案是正數、乘法的答案最多為 100,而除法的答案不是分數。

新增 [開始] 按鈕的事件處理常式

在本章節中,您會新增程式碼,以在選取 [開始] 按鈕時啟動測驗。 回應按鈕選取之類的事件所執行的程式碼稱為事件處理常式。

  1. Windows Forms 設計工具中,按兩下 [開始測驗] 按鈕,或選取該按鈕然後選取 [Enter]。 表單的程式碼隨即出現,而且可以看到新的方法。

    這些動作會將 [點擊] 事件處理常式新增至 [開始] 按鈕。 當接受測驗者選取此按鈕時,應用程式會執行您要新增至這個新方法的程式碼。

  2. 新增下列兩個語句,讓事件處理常式啟動測驗。

    private void startButton_Click(object sender, EventArgs e)
    {
        StartTheQuiz();
        startButton.Enabled = false;           
    }
    

第一個陳述式會呼叫新的 StartTheQuiz() 方法。 第二個陳述式會將 [startButton] 控制項的 [已啟用] 屬性設定為 false,如此接受測驗者就無法在測驗期間選擇該按鈕。

執行您的應用程式

  1. 儲存您的程式碼。

  2. 執行您的應用程式,然後選取 [啟動測驗]。 隨機的數學問題隨即出現,如下列螢幕擷取畫面所示。

    Screenshot that shows random values in all four math problems. The Start the quiz button appears dimmed.

下一步

前進到下一個教學課程,將計時器新增至您的數學測驗,並檢查使用者答案。