谢谢你的解答!- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Combox
- {
- public partial class 简单计算器 : Form
- {
- public 简单计算器()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string str1 = txtNumber1.Text.Trim();
- string str2 = txtNumber2.Text.Trim();
- double i1, i2; //定义成double类型,方便小数输入输出
- double result;
- if (double.TryParse(str1, out i1) == false || double.TryParse(str2, out i2) == false)
- {
- MessageBox.Show("请输入数字","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
- return;
- }
- switch(comboBox1.SelectedIndex)
- {
- case 1:
- result= i1 + i2;
- break;
- case 2:
- result = i1 - i2;
- break;
- case 3:
- result = i1 * i2;
- break;
- case 4:
- if (i2 == 0)
- {
- MessageBox.Show("0不能为除数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- result = i1 * 1.0 / i2;
- break;
- case 5:
- if (i2 == 0)
- {
- MessageBox.Show("0不能为除数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- result = i1 % i2;
- break;
-
- default:
- MessageBox.Show("无效的运算,请选择运算符", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
-
- txtResult.Text = Convert.ToString(result);
- }
- private void 简单计算器_Load(object sender, EventArgs e)
- {
- comboBox1.SelectedIndex = 0; //默认显示请选择运算符
- }
-
- }
- }
复制代码 这是我的代码,看看还有什么不足之处 |