题目:窗体程序完成加减乘除四则运算,并将结果输出到对话框。public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();   
        }        private void 加法ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //input number validation
            double num1, num2,result;            bool val=InputValidation(txtNumberOne.Text, txtNumberTwo.Text);
            
            if (val)
            {
                //calculation
                double.TryParse(txtNumberOne.Text, out num1);
                double.TryParse(txtNumberTwo.Text, out num2);
                result = num1 + num2;                //output result
                MessageBox.Show(result.ToString());
            }
            else
            {
                MessageBox.Show("The input numbers are not valid,please try again.");
            }
        }
        private void 减法ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //input number validation
            double num1, num2, result;            bool val = InputValidation(txtNumberOne.Text, txtNumberTwo.Text);            if (val)
            {
                //calculation
                double.TryParse(txtNumberOne.Text, out num1);
                double.TryParse(txtNumberTwo.Text, out num2);
                result = num1 - num2;                //output result
                MessageBox.Show(result.ToString());
            }
            else
            {
                MessageBox.Show("The input numbers are not valid,please try again.");
            }
        }        private void 乘法ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //input number validation
            double num1, num2, result;            bool val = InputValidation(txtNumberOne.Text, txtNumberTwo.Text);            if (val)
            {
                //calculation
                double.TryParse(txtNumberOne.Text, out num1);
                double.TryParse(txtNumberTwo.Text, out num2);
                result = num1 * num2;                //output result
                MessageBox.Show(result.ToString());
            }
            else
            {
                MessageBox.Show("The input numbers are not valid,please try again.");
            }
        }        private void 除法ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //input number validation
            double num1, num2, result;            bool val = InputValidation(txtNumberOne.Text, txtNumberTwo.Text);            if (val)
            {
                //calculation
                double.TryParse(txtNumberOne.Text, out num1);
                double.TryParse(txtNumberTwo.Text, out num2);                if (num2 != 0)
                {
                    result = num1 / num2;
                    //output result
                    MessageBox.Show(result.ToString());
                }
                else
                {
                    MessageBox.Show("除数不可以为0,请重新输入!");
                }            }
            else
            {
                MessageBox.Show("The input numbers are not valid,please try again.");
            }
        }
        private bool InputValidation(string _num1, string _num2)
        {
            double num1, num2;
            bool val = false;
            try
            {
                val = (double.TryParse(_num1, out num1)) && (double.TryParse(_num2, out num2));
            }
            catch
            {
                new Exception();
            }
            return val;        }
    }代码还可以更简练些吗?请大家不吝赐教。另外,我没完成练习题15,是关于委托和事件的,还在研究中。稍后把代码发上来,请大家帮我参详参详。先谢啦!

解决方案 »

  1.   

    CSDN告诉我
    每天回帖即可获得10分可用分!
      

  2.   

    double num1, num2,result; 你这些参数都声明在外面也行的啊,不用每次都声明
      

  3.   

    一个方法就可以避免那么多的重复代码了,看下面的
    public void compute(string strType)  //strType就是运算符号
    {
         double num1, num2, result; 
         bool val = InputValidation(txtNumberOne.Text, txtNumberTwo.Text); 
         if (val) 
         { 
             //calculation 
              double.TryParse(txtNumberOne.Text, out num1); 
              double.TryParse(txtNumberTwo.Text, out num2); 
              result = num1 + strType + num2;           //output result 
              MessageBox.Show(result.ToString()); 
          } 
          else 
          { 
             MessageBox.Show("The input numbers are not valid,please try again."); 
             return;
          } }然后在