窗体四则运算。

我用的是窗体来做的  老师教过 比较简单的方法。

代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace szys.c

{   

  public partial class Form1 : Form  

   {  

       public Form1()      

   {          

   InitializeComponent();      

   }

       private static int     

        Count = 0,     

       right = 0;       

      private void ys()     

    {        

     Random rn = new Random();      

       int i, j;     

        i = rn.Next(11);     

        j = rn.Next(1, 11);         

        textBox1.Text = i.ToString();

         textBox2.Text = j.ToString();    

         textBox3.Text = "";     

       }     

private void button1_Click_1(object sender, EventArgs e)     

    {        

     label1.Text = "+";       

      label1.Visible = true;       

  }

        private void button2_Click_1(object sender, EventArgs e)     

    {          

                     label1.Text = "-";       

                   label1.Visible = true;     

    }

        private void button3_Click(object sender, EventArgs e)       

  {          

                    label1.Text = "*";        

                            label1.Visible = true;   

      }

        private void button4_Click(object sender, EventArgs e)       

  {         

                      label1.Text = "/";         

                         label1.Visible = true;   

      }   

   private void button5_Click_1(object sender, EventArgs e)

        {      

                   ys();       

              } 

    private void button6_Click_1(object sender, EventArgs e)      

   {     

        textBox3.Enabled = false;   

          MessageBox.Show("运算结束!");   

      }         

private void textBox3_KeyDown_1(object sender, KeyEventArgs e)   

      {

                 if (label1.Text == "+")      

       {                

                         int sum;         

                      sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);   

              if (e.KeyCode == Keys.Enter)            

     {              

       if (textBox3.Text == sum.ToString())  

                   {       right++;         

                         Count++;            

                                  }             

        else                

                 Count++;    

                 textBox4.Text = Count.ToString();                                    

                 textBox4.Enabled = false;

                  textBox5.Text = right.ToString();

                  textBox5.Enabled = false;                  

                ys();      

           }         

    }      

  

        else if (label1.Text=="-")     

        {     int cha;     

            cha = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);   

              if (e.KeyCode == Keys.Enter)     

            {             

        if (textBox3.Text == cha.ToString())     

                {             

            right++;               

          Count++;                

     }                   

  else          

               Count++;      

               textBox4.Text = Count.ToString();    

                 textBox4.Enabled = false;       

              textBox5.Text = right.ToString();     

                textBox5.Enabled = false;                     

                 ys();            

          }        

     }

           else  if (label1.Text=="*")     

        {               

              int qj;        

         qj  = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);   

              if (e.KeyCode == Keys.Enter)       

          {                 

    if (textBox3.Text == qj.ToString())    

                 {              

           right++;             

            Count++;          

           }          

           else         

                Count++;      

               textBox4.Text = Count.ToString();  

                   textBox4.Enabled = false;

                    textBox5.Text = right.ToString();   

                  textBox5.Enabled = false; 

                 ys();        

         }            

}           

  if (label1.Text=="/")     

        {           

      double chu;         

        chu  = int.Parse(textBox1.Text) /int.Parse(textBox2.Text);  

               if (e.KeyCode == Keys.Enter)      

           {      

               if (double.Parse (textBox3.Text)==chu )   

                  {    

                     right++;   

                      Count++;  

                   }           

          else                  

       Count++;            

         textBox4.Text = Count.ToString();    

                 textBox4.Enabled = false;      

               textBox5.Text = right.ToString();   

                  textBox5.Enabled = false; 

              }           

        }            

        }

        }

}

思路就是用窗体构建出模型,然后编写代码,用时1小时。

原文地址:https://www.cnblogs.com/houqiqi/p/4852511.html