C#- 设计一个简单的计算器,使其可以加减乘除计算

C#- 设计一个简单的计算器,使其可以加减乘除计算_第1张图片
C#- 设计一个简单的计算器,使其可以加减乘除计算

实现效果图:C#- 设计一个简单的计算器,使其可以加减乘除计算_第2张图片
C#- 设计一个简单的计算器,使其可以加减乘除计算_第3张图片
如果输入有误,会报错。

代码如下:`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
int flag = 0;
public Form1()
{
InitializeComponent();
}

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        flag = 1;
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        flag = 2;
    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
        flag =3;
    }

    private void radioButton4_CheckedChanged(object sender, EventArgs e)
    {
        flag =4;
    }

    private void add()
    {
        double n1 = Convert.ToDouble(textBox1.Text);
        double n2 = Convert.ToDouble(textBox2.Text);
        textBox3.Text = Convert.ToString(n1 + n2);
    }

    private void sub()
    {
        double n1 = Convert.ToDouble(textBox1.Text);
        double n2 = Convert.ToDouble(textBox2.Text);
        textBox3.Text = Convert.ToString(n1-n2);
    }

    private void mult()
    {
        double n1 = Convert.ToDouble(textBox1.Text);
        double n2 = Convert.ToDouble(textBox2.Text);
        textBox3.Text = Convert.ToString(n1*n2);
    }

    private void div()
    {
        double n1 = Convert.ToDouble(textBox1.Text);
        double n2 = Convert.ToDouble(textBox2.Text);
        textBox3.Text = Convert.ToString(n1/n2);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == ""||flag==0)
        {
            MessageBox.Show("请检查输入", "提示", MessageBoxButtons.OK);
        }
        else
        {
            switch (flag)
            {
                case 1: add(); break;
                case 2: sub(); break;
                case 3: mult(); break;
                case 4: div(); break;
                default: break;
            }
        }

    }
}

}
`

如果对你有帮助,点个赞吧!

你可能感兴趣的:(C#)