共用方式為


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

在本系列四個教程中,您將製作數學測驗。 測驗包含四個隨機數學問題,測驗者會嘗試在指定的時間內回答。

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

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

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

先決條件

本教學課程基於之前的教學課程,建立數學測驗 WinForms 應用程式。 如果您尚未完成該教程,請先完成。

建立隨機加法問題

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

  2. 選取表單 表單1

  3. 在功能表欄上,選取 檢視>程式碼。 視您使用的程式設計語言而定,Form1.csForm1.vb 出現,以便檢視表單背後的程序代碼。

  4. Form1.csForm1.vb中,在程式代碼頂端附近新增 new 語句,以建立 Random 物件。

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

您可以使用類似此語句的 new 語句來建立按鈕、卷標、面板、OpenFileDialogs、ColorDialogs、SoundPlayers、Randoms,甚至是表單。 這些項目稱為 物件。

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

您的測驗需要變數來儲存針對每個問題建立的隨機數。 使用變數之前,您可以宣告變數,這表示列出其名稱和數據類型。

  1. 將兩個整數變數新增至窗體,並將其命名 addend1addend2Form1.csForm1.vb中。

    注意

    整數變數稱為 C# 中的 int 或 Visual Basic 中的 Integer。 這種變數會儲存 -2147483648 到2147483647的正數或負數,而且只能儲存整數,而非小數。

    您可以使用類似的語法來新增整數變數,就像新增 Random 對象一樣,如下列程式代碼所示。

    // 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()Form1.csForm1.vb的方法。 此方法會使用 Random 物件的 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() 方法與 Random 物件搭配使用時,例如當您呼叫 randomizer.Next(51)時,您會收到小於 51 或介於 0 到 50 之間的隨機數位。 此程式代碼會呼叫 randomizer.Next(51),讓兩個隨機數位加總到介於 0 到 100 之間的答案。

仔細看看這些語句。

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

這些語句 會設定 plusLeftLabelplusRightLabel 的 text 属性,讓它們顯示兩個隨機數。 標籤控制項會以文字格式顯示值,以及在程式設計中,字串會保留文字。 每個整數 ToString() 方法會將整數轉換成標籤可以顯示的文字。

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

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

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

    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. 藉由新增下列程式代碼,從「填入減法問題」批注開始,修改 Form1.csForm1.vb 中的 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 控件的 Enabled 屬性設定為 false,讓測驗接受者無法在測驗期間選取按鈕。

執行您的應用程式

  1. 儲存您的程序代碼。

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

    顯示所有四個數學問題的隨機值的螢幕快照。[開始測驗] 按鈕會顯示為暗灰色。

後續步驟

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