博客
关于我
C# —— 使用WPF搭建小型计算器
阅读量:298 次
发布时间:2019-03-03

本文共 4845 字,大约阅读时间需要 16 分钟。

C# —— 使用WPF搭建小型计算器

  • 使用try catch语句捕获(除数为0,参数为空,参数不是数字,数据溢出)这四个异常类。捕获异常后再textblock输出文本框中显示。
  • 定义“数学”类,实现加减乘除四个函数。
  • 界面用两个输入文本框,加减乘除四个按钮,一个textblock输出文本框。

参数为空报错:

在这里插入图片描述
参数不是数字:
在这里插入图片描述
数据溢出:
在这里插入图片描述除数为0:
在这里插入图片描述

XAML文件:

CS文件:

using System;using System.Collections.Generic;using System.Data.SqlTypes;using System.Linq;using System.Runtime.CompilerServices;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApp3{       public partial class MainWindow : Window    {           public MainWindow()        {               InitializeComponent();        }        private void Button_Click(object sender, RoutedEventArgs e)        {               double x, y;            MyMath M = new MyMath();            textbox3.Clear();            if (textbox1.Text == "" || textbox2.Text == "")            {                   try                {                       x = double.Parse(textbox1.Text);                    y = double.Parse(textbox2.Text);                }                catch (Exception)                {                       textbox3.Text += "错误:参数为空";                }            }            else            {                   try                {                       x = double.Parse(textbox1.Text);                    y = double.Parse(textbox2.Text);                    if (jiafa.IsChecked == true)                    {                           textbox3.Text = M.GetHe(x, y).ToString();                    }                    else if (jianfa.IsChecked == true)                    {                           textbox3.Text = M.GetCha(x, y).ToString();                    }                    else if (chengfa.IsChecked == true)                    {                           textbox3.Text = M.GetJi(x, y).ToString();                    }                    else if (chufa.IsChecked == true)                    {                           try                        {                               textbox3.Text = M.GetShang(x, y).ToString();                        }                        catch (DivideByZeroException err)                        {                               textbox3.Text += err;                        }                    }                }                catch (FormatException err)                {                       textbox3.Text += err;                }                catch (OverflowException err)                {                       textbox3.Text += err;                }            }        }        private void jiafa_Click(object sender, RoutedEventArgs e)        {               if (jiafa.IsChecked == true)            {                   label1.Content = "+";                groupbox.Header = "加法";            }            textbox1.Text = "";            textbox2.Text = "";            textbox3.Text = "";        }        private void jianfa_Click(object sender, RoutedEventArgs e)        {               if (jianfa.IsChecked == true)            {                   label1.Content = "-";                groupbox.Header = "减法";            }            textbox1.Text = "";            textbox2.Text = "";            textbox3.Text = "";        }        private void chengfa_Click(object sender, RoutedEventArgs e)        {               if (chengfa.IsChecked == true)            {                   label1.Content = "*";                groupbox.Header = "乘法";            }            textbox1.Text = "";            textbox2.Text = "";            textbox3.Text = "";        }        private void chufa_Click(object sender, RoutedEventArgs e)        {               if (chufa.IsChecked == true)            {                   label1.Content = "/";                groupbox.Header = "除法";            }            textbox1.Text = "";            textbox2.Text = "";            textbox3.Text = "";        }            }    class MyMath    {           public double GetHe(double numA, double numB)        {               return numA + numB;        }        public double GetCha(double numA, double numB)        {               return numA - numB;        }        public double GetShang(double numA, double numB)        {               if (numB == 0) throw new DivideByZeroException();            return numA / numB;        }        public double GetJi(double numA, double numB)        {               return numA * numB;        }    }}

转载地址:http://fpil.baihongyu.com/

你可能感兴趣的文章
MySQL 设置数据库的隔离级别
查看>>
MySQL 证明为什么用limit时,offset很大会影响性能
查看>>
Mysql 语句操作索引SQL语句
查看>>
MySQL 误操作后数据恢复(update,delete忘加where条件)
查看>>
MySQL 调优/优化的 101 个建议!
查看>>
mysql 转义字符用法_MySql 转义字符的使用说明
查看>>
mysql 输入密码秒退
查看>>
mysql 递归查找父节点_MySQL递归查询树状表的子节点、父节点具体实现
查看>>
mysql 通过查看mysql 配置参数、状态来优化你的mysql
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>
Mysql 重置自增列的开始序号
查看>>
mysql 锁机制 mvcc_Mysql性能优化-事务、锁和MVCC
查看>>
MySQL 错误
查看>>
mysql 随机数 rand使用
查看>>
MySQL 面试题汇总
查看>>
MySQL 面试,必须掌握的 8 大核心点
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>