本文共 4845 字,大约阅读时间需要 16 分钟。
参数为空报错:
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/