步驟 7:加入乘法和除法問題
開始進行此程序之前,請先思考應該如何加入乘法和除法問題。 思考初始步驟,其中牽涉到儲存值。
若要加入乘法和除法問題
將四個 int (Integer) 加入至表單。 程式碼看起來應該如下所示。
Public Class Form1 ' Create a Random object to generate random numbers. Private randomizer As New Random ' These Integers will store the numbers in the addition problem. Private addend1 As Integer Private addend2 As Integer ' These Integers will store the numbers for the subtraction problem. Private minuend As Integer Private subtrahend As Integer ' These Integers will store the numbers for the multiplication problem. Private multiplicand As Integer Private multiplier As Integer ' These Integers will store the numbers for the division problem. Private dividend As Integer Private divisor As Integer ' This Integer will keep track of the time left. Private timeLeft As Integer
public partial class Form1 : Form { // Create a Random object to generate random numbers. Random randomizer = new Random(); // These ints will store the numbers for the addition problem. int addend1; int addend2; // These ints will store the numbers for the subtraction problem. int minuend; int subtrahend; // These ints will store the numbers for the multiplication problem. int multiplicand; int multiplier; // These ints will store the numbers for the division problem. int dividend; int divisor; // This int will keep track of the time left. int timeLeft;
如同先前一樣,修改 StartTheQuiz() 方法來填入隨機的乘法和除法問題。 程式碼看起來應該如下所示。
''' <summary> ''' Start the quiz by filling in all of the problems ''' and starting the timer. ''' </summary> ''' <remarks></remarks> Public Sub StartTheQuiz() ' Fill in the addition problem. addend1 = randomizer.Next(51) addend2 = randomizer.Next(51) plusLeftLabel.Text = addend1.ToString() plusRightLabel.Text = addend2.ToString() 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) Dim temporaryQuotient As Integer = randomizer.Next(2, 11) dividend = divisor * temporaryQuotient dividedLeftLabel.Text = dividend.ToString() dividedRightLabel.Text = divisor.ToString() quotient.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 problems /// and starting the timer. /// </summary> public void StartTheQuiz() { // Fill in the addition problem. addend1 = randomizer.Next(51); addend2 = randomizer.Next(51); plusLeftLabel.Text = addend1.ToString(); plusRightLabel.Text = addend2.ToString(); 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; // Start the timer. timeLeft = 30; timeLabel.Text = "30 seconds"; timer1.Start(); }
修改 CheckTheAnswer() 方法,讓方法也檢查乘法和除法問題。 程式碼看起來應該如下所示。
''' <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 AndAlso minuend - subtrahend = difference.Value AndAlso multiplicand * multiplier = product.Value AndAlso dividend / divisor = quotient.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) && (minuend - subtrahend == difference.Value) && (multiplicand * multiplier == product.Value) && (dividend / divisor == quotient.Value)) return true; else return false; }
注意事項 因為使用鍵盤不容易輸入乘號 (×) 和除號 (÷),所以 Visual C# 和 Visual Basic 使用星號 (*) 表示乘法,使用斜線符號 (/) 表示除法。
變更計時器的 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 product.Value = multiplicand * multiplier quotient.Value = dividend / divisor 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; product.Value = multiplicand * multiplier; quotient.Value = dividend / divisor; startButton.Enabled = true; }
儲存並執行您的程式。 現在,必須回答四個問題才能完成測驗,如下列圖片所示。
含有四個問題的數學測驗
若要繼續或檢視
若要移到下一個教學課程步驟,請參閱步驟 8:嘗試其他功能。
若要回到上一個教學課程步驟,請參閱步驟 6:加入減法問題。