4.数学检测

编辑器加载中...

4.数学检测

  新建Windows窗体应用程序
      Text -数学检测            Size 500*400 
      FormBorderStyle-Fixed3D   MaximizeBox-False

  添加标签控件  Name-timeLabel     AutoSize-False     BorderStyle-FixedSingle
                Size设置为200,30   Text 为空          字号-15.75  
  标签拖动到窗体的右上角直到出现蓝色分隔线

  添加另一个标签控件 字号-15.75   Text-剩余时间  拖动使其与timeLabel左侧对齐

     为加法问题添加标签控件
  将Text 属性-?(问号)       AutoSize-False      Size-60,50          字号-18
    TextAlign-MiddleCenter  Location-75,75      Name-plusLeftLabel

    复制标签
  选择plusLeftLabel标签,复制粘贴三次,将这三个新标签在plusLeftLabel的右侧排成一行,使用分隔线将其隔开对齐,第二个标签的Text属性为 +(加号);第三个标签的Name-plusRightLabel ;第四个标签的Text- =(等号);

    添加NumericUpDown控件
  字号-18   宽度-100    拖动与加法问题的Label控件排成一行  Name-sum

    复制粘贴加法问题控件
  选择加法问题中的所有五个控件并复制粘贴,向窗体添加五个新的控件,使其在加法控件下方排成一行,使用分割线保持距离.
  第二个标签的Text为-(减号),第一个问号标签命名为minusLeftLabel
  第二个问号标签命名为-minusRightLabel
   NumericUpDown标签命名为-difference    
  再次复制黏贴控件
  第一个标签命名为-timesLeftLabel
  第二个标签的Text属性更改为*(乘号)
  第三个标签命名为-timesRightLabel
   NumericUpDown控件命名为product
  再次复制粘贴控件
  第一个标签命名-dividedLeftLabel
  第二个标签的Text-/(除号)
  第三个标签命名-dividedRightLabel
   NumericUpDown-quotient

   添加按钮  name--startButton    Text-开始检测   字号-14
             AutoSize-True
   将按钮拖动到窗体底部,并将其居中对齐

   将 startButton 的 TabIndex 设置为 1   将 sum 控件设置为 2
   将 difference 控件设置为 3
   将 product 控件设置为 4
  
将 quotient 控件设置为 5  

   在class Form1中写入
   //向窗体添加 Random对象并称作randomizer
   Random randomizer = new Random();
   //添加整型int变量称作addend1和addend2
   private int addend1;
   private int addend2;
 
   //添加 StartTheQuiz()的方法
   private void StartTheQuiz()
        {  //add
            addend1 = randomizer.Next(51);
            addend2 = randomizer.Next(51);
           
            plusLeftLabel.Text = addend1.ToString();
            plusRightLabel.Text = addend2.ToString();

            sum.Value = 0;     
        }
-----------------------------------------------------------------------
        private void startbutton_Click(object sender, EventArgs e)
        {
            startButton.Enabled = false;
            //调用 tartTheQuiz()方法
            StartTheQuiz();
        }
选择startButton按钮 Click-startbutton_Click              

   添加倒计时计时器
   添加Timer组件   Interval -1000    
   添加 Timer 组件的Tick 事件--
        (在class Form1中写入)private int timeLeft;

 private void timer1_Tick(object sender, EventArgs e)
        {           
            if (timeLeft > 0)
            {
                timeLeft = timeLeft - 1;
                timeLabel.Text = timeLeft + @"秒";
                //timeLabel.Text = timeLeft + "seconds";            
            }
            else
            {
                timer1.Stop();
                timeLabel.Text = @"时间到";
                MessageBox.Show("You didn't finish in time.","Sorry");
                sum.Value = addend1 + addend2;            
                startButton.Enabled = true;
            }
        }
------------------------------------------------------------------------
        private void StartTheQuiz()
        {  //add
            addend1 = randomizer.Next(51);
            addend2 = randomizer.Next(51);           
            plusLeftLabel.Text = addend1.ToString();
            plusRightLabel.Text = addend2.ToString();
            sum.Value = 0; 
   
           //set time
            timeLeft = 20;
            timeLabel.Text = timeLeft + @"20秒";
            //timeLabel.Text = "30 seconds";
            timer1.Start();
        }
---------------------------------------------------------------------------------
CheckTheAnswer()方法
    private bool CheckTheAnswer()
        {       
            if(addend1 + addend2 == sum.Value)             
               return true;          
            else           
              return false;
        }
--------------------------------------------------------
 private void timer1_Tick(object sender, EventArgs e)
        {
            //多加了if(CheckTheAnswer())
            if (CheckTheAnswer())
            {
                timer1.Stop();
                MessageBox.Show("You got all the answers right!","Congratulation");
                startButton.Enabled = true;
            }
            else if (timeLeft > 0)
            {
                //timeLeft = timeLeft - 1;
                timeLeft--;
                timeLabel.Text = timeLeft + "seconds";
            
            }
            else
            {
                timer1.Stop();
                timeLabel.Text = "Time's up!";
                MessageBox.Show("You didn't finish in time.","Sorry");
                sum.Value = addend1 + addend2;               
                startButton.Enabled = true;
            }
        }
-----------------------------------------------------------
选择多个NumericUpDown 控件属性- Enter 事件- answer_Enter

 private void answer_Enter(object sender, EventArgs e)
        {
            NumericUpDown answerBox = sender as NumericUpDown;
           
             if(answerBox != null)
             {
                 int lengthOfAnswer = answerBox.Value.ToString().Length;
                 answerBox.Select(0,lengthOfAnswer);
             }
        }
添加减法问题
        private int subtrahend;
        private int minuend;
修改StartTheQuiz()方法
        //sub
            minuend = randomizer.Next(1,101);
            subtrahend = randomizer.Next(1,minuend);
            minusLeftLabel.Text = minuend.ToString();
            minusRightLabel.Text = subtrahend.ToString();
            difference.Value = 0;      
修改CheckTheAnswer()方法
       if((addend1 + addend2 == sum.Value)
                &&(minuend - subtrahend == difference.Value))
更改Tick事件处理程序
             else
            {
                timer1.Stop();
                timeLabel.Text = @"时间到";
                MessageBox.Show(@"您没能在规定时间完成", @"对不起");
                sum.Value = addend1 + addend2;
                difference.Value = minuend - subtrahend;
                startButton.Enabled = true;
            }
添加整型变量
        private int multiplicand;
        private int multiplier;

        private int dividend;
        private int divisor;

修改StartTheQuiz()方法
            //multiple
            multiplicand = randomizer.Next(2, 11);
            multiplier = randomizer.Next(2, 11);           
            //multiplier =randomizer.Next(100/multiplicand);
            timesLeftLabel.Text = multiplicand.ToString();
            timesRightLabel.Text = multiplier.ToString();
            product.Value = 0;

            //divide
            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;
修改CheckTheAnswer()方法
        if((addend1 + addend2 == sum.Value)
                &&(minuend - subtrahend == difference.Value)
                &&(multiplicand * multiplier == product.Value)
                &&(dividend / divisor == quotient.Value))
更改Tick事件处理程序
else
            {
                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;  
            }

posted @ 2012-04-03 21:06  珍爱贝贝1314  阅读(191)  评论(1编辑  收藏  举报