C# 如何根本性解决中double类型的非空检测 C#计算器

本人大一,学习C#知识时遇到这样一个问题,win10自带的计算器无法进行多项计算,所以想自己写个,的mun1判断的非空问题而无法继续往下写了,下面为运算代码和截图,请csdn大佬支个招

图片说明

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    double reasult=0,memory=0.0;
    double num2,num1;
    double oper;
    string temp;
    private void Form1_Load(object sender, EventArgs e)
    {
        this.Text = "计算器";


    }

    private void Num(object sender, EventArgs e)
    {
        Button a = (Button)sender;//判断数值按钮实现数值输入
        if (numout.Text == "0")
        {
            numout.Text = a.Text;
        }
        else
        {
            numout.Text += a.Text;
        }
    }

    private void yunshuan(object sender, EventArgs e)//实现运算公式
    {
        if (numout.Text != "")
        {
            temp = num1.ToString();
            if (temp != "")
            {

                num2 = double.Parse(numout.Text);
                switch (oper)
                {
                    case '+': num1 = num1 + num2; break;
                    case '-': num1 = num1 - num2; break;
                    case '*': num1 = num1 * num2; break;
                    case '÷':
                        if (num2 == 0)
                        {
                            MessageBox.Show("0不能为除数");
                            break;
                        }
                        else
                        {
                            num1 = num1 / num2; break;
                        }

                }
                 oper = char.Parse(((Button)sender).Text);
                numout.Text = "";
            }
            else
            {
                 numout.Text = num1.ToString();
                 num1 = double.Parse(numout.Text);
                 oper = char.Parse(((Button)sender).Text);
                 numout.Text = "";
                 foot.Text += "运行一次";
            }
        }
    }

    private void button16_Click(object sender, EventArgs e)
    {
        /*
        if (numout.Text != "")
        {
            num2 = double.Parse(numout.Text);
            switch (oper)
            {
                case '+': reasult = num1 + num2; break;
                case '-': reasult = num1 - num2; break;
                case '*': reasult = num1 * num2; break;
                case '÷': if (num2 == 0)
                    {
                        MessageBox.Show("0不能为除数");
                        break;
                    }
                    else
                    {
                        reasult = num1 / num2;break;
                    }

            }

        }
        */
        numout.Text = num1.ToString();
    }

    private void button11_Click(object sender, EventArgs e)
    {

    }
    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {

    }
}

}

2个回答

double是不可以为空的,所以不需要判断 num1 是不是为 null

我试了,if(num!=null)//vs程序提示该表达式始终为true

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题