步骤 6:添加减法问题
若要添加减法问题,您需要:
存储做减法的值。
生成用于提问的随机数字,并确保答案介于 0 和 100 之间。
更新用于检查答案的方法,以便该方法也会检查新的减法问题。
更新计时器的 Tick 事件处理程序,以便此事件处理程序在时间用完之前会填写出正确答案。
添加减法问题
首先,需要一个位置来存储值,以便将用于减法问题的两个 int (Integer) 变量添加到窗体中。新的代码出现在加法整数和计时器整数之间。代码应如下所示。
Public Class Form1 ' Create a Random object to generate random numbers. Private randomizer As New Random ' These Integers will store the numbers ' for 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 ' 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; // This int will keep track of the time left. int timeLeft;
说明 新的 int 变量含义 和 减数—不是编程术语的名称。它们是算术中的传统名称,分别是指要减去的数字(subtrahend,减数)和其中减去另一个数字的数字(minuend,被减数)。差值是被减数减去减数的结果。您可以使用其他名称,因为程序不要求 int 变量、控件、组件或方法具有特定的名称。存在一些规则(例如,名称不能以数字开头),但一般情况下,您可以使用 x1、x2、x3、x4 等这样的名称。不过,这样的命名方式会使阅读代码变得很困难,并且几乎不可能跟踪习题。在本教程后面部分,将对乘法(被乘数 × 乘数 = 积)和除法(被除数 ÷ 除数 = 商)使用传统名称。
下一步,修改 StartTheQuiz() 方法以填写随机减法问题。新代码后跟“Fill in the subtraction problem”(填写减法问题)注释。代码应如下所示。
''' <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 ' 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; // Start the timer. timeLeft = 30; timeLabel.Text = "30 seconds"; timer1.Start(); }
此代码对 Random 类的 Next() 方法的用法稍有不同。当您给出两个值时,该方法会选取一个大于或等于第一个值但小于第二个值的随机数。下面的行从 1 到 100 之间选择一个随机数,并将其存储在 minuend 变量中。
minuend = randomizer.Next(1, 101)
minuend = randomizer.Next(1, 101);
可以使用多种方式调用 Random 类的 Next() 方法。可以使用多种方式调用一个方法的情况称为“重载方法”,您可以使用 IntelliSense 来探索此情况。另外,请查看**“IntelliSense”**窗口工具提示中对 Next() 方法的说明。
Intellisense 窗口工具提示
请注意工具提示是如何显示**“(+ 2 重载)”**的。这意味着您还可以通过另外两种方式来调用 Next() 方法。在为 StartTheQuiz() 方法键入新代码时,您可以看到更多信息。键入 randomizer.Next(, 后,IntelliSense 窗口将打开。按向上键和向下键可循环访问重载,如下图中所示。
Intellisense 窗口重载
上图中显示的重载即是您所需要的,因为通过此重载您可以指定最小值和最大值。
修改 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 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)) return true; else return false; }
&& 是 Visual C# logical and 运算符。在 Visual Basic 中,等效的运算符是 AndAlso。它的含义等同于“如果 addend1 加上 addend2 等于 sum NumericUpDown 控件的值,并且 minuend 减去 subtrahend 等于 difference NumericUpDown 控件的值”。只有在加法问题和减法问题都正确时,CheckTheAnswer() 方法才会返回 true。
更改计时器的 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; }
保存并运行代码。现在,您的程序应包含一个减法问题,如下图中所示。
包含减法问题的数学测验
继续或查看
若要转到下一个教程步骤,请参见步骤 7:添加乘法和除法问题。
若要返回上一个教程步骤,请参见步骤 5:为 NumericUpDown 控件添加 Enter 事件处理程序。