当前位置:编程学习 > C#/ASP.NET >>

C#学习2

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


[csharp]
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; 
            } 
        } 
 
    } 

 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,