C#学习2

来源:互联网 发布:怎么查淘宝号安全 编辑:程序博客网 时间:2024/06/08 08:01

写了一个简单的计算器,能实现加减乘除,刚开始没把函数和button联系在一起,调试了半天哭

namespace math{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void addvalue()        {            int l = int.Parse(left.Text);            int r = int.Parse(right.Text);            int outcome;            outcome = l + r;            expression.Text = left.Text + "+" + right.Text;            result.Text = outcome.ToString();        }        private void subvalue()        {            int l = int.Parse(left.Text);            int r = int.Parse(right.Text);            int outcome;            outcome = l - r;            expression.Text = left.Text + "-" + right.Text;            result.Text = outcome.ToString();        }        private void mulvalue()        {            int l = int.Parse(left.Text);            int r = int.Parse(right.Text);            int outcome;            outcome = l * r;            expression.Text = left.Text + "*" + right.Text;            result.Text = outcome.ToString();        }        private void divvalue()        {            double l = double.Parse(left.Text);            double r = double.Parse(right.Text);            double outcome;            outcome = l / r;            expression.Text = left.Text + "/" + right.Text;            result.Text = outcome.ToString();        }        private void quit_Click(object sender, RoutedEventArgs e)        {            this.Close();        }        private void calculate_Click(object sender, RoutedEventArgs e)        {            try            {                if ((bool)add.IsChecked)                    addvalue();                else if ((bool)sub.IsChecked)                    subvalue();                else if ((bool)mul.IsChecked)                    mulvalue();                else if ((bool)div.IsChecked)                    divvalue();            }            catch (Exception caught)  //捕获异常            {                expression.Text = "";                result.Text = caught.Message;            }        }    }}