Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c中的减法运算出错#_C#_.net_Subtraction - Fatal编程技术网

C# c中的减法运算出错#

C# c中的减法运算出错#,c#,.net,subtraction,C#,.net,Subtraction,我正在用c#编写一个程序。我已经写了三个数字的减法逻辑,但是它显示了错误的输出。下面是我的代码。任何帮助都将不胜感激 private void btnSub_Click(object sender, EventArgs e) { if (ch != "-") { num1 = num1 - double.Parse(textBox1.Text); } else {

我正在用c#编写一个程序。我已经写了三个数字的减法逻辑,但是它显示了错误的输出。下面是我的代码。任何帮助都将不胜感激

private void btnSub_Click(object sender, EventArgs e)
    {
        if (ch != "-")
        {
            num1 =  num1 - double.Parse(textBox1.Text);
        }
        else
        {
            num1= Convert.ToInt32(textBox1.Text);
            ch = "";
        }

        textBox1.Text = "";

        op = "-";

        textBox1.Text += op;

    }
我的完整代码:

namespace Cal1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static double num1, num2 = 0;
        string op;
        static string ch = "";


        private void button1_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Text = "";

                textBox1.Text = textBox1.Text + button1.Text;
            }
            else
            {
                textBox1.Text = textBox1.Text + button1.Text;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Text = "";

                textBox1.Text = textBox1.Text + button2.Text;
            }
            else
            {
                textBox1.Text = textBox1.Text + button2.Text;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button3.Text;
            }
            else
            textBox1.Text = textBox1.Text + button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button4.Text;
            }
            else
                textBox1.Text = textBox1.Text + button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button5.Text;
            }
            else
                textBox1.Text = textBox1.Text + button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button6.Text;
            }
            else
                textBox1.Text = textBox1.Text + button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button7.Text;
            }
            else
                textBox1.Text = textBox1.Text + button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button8.Text;
            }
            else
                textBox1.Text = textBox1.Text + button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button9.Text;
            }
            else
                textBox1.Text = textBox1.Text + button9.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
            {
                textBox1.Clear();
                textBox1.Text = textBox1.Text + button10.Text;
            }
            else
                textBox1.Text = textBox1.Text + button10.Text;
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            double result;
            num2 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            switch (op)
            {
                case "+":
                    result = num1 + num2;
                    textBox1.Text += result;
                    num1 = result;
                    ch = "+";
                    break;
                case "-":
                    result = num1 - num2;
                    textBox1.Text += result;
                    num1 = result;
                    ch = "-";
                    break;
                case "*":
                    result = num1 * num2;
                    textBox1.Text += result;
                     num1 = result;
                    ch = "*";
                    break;
                case "/":
                    result = num1 / num2;
                    textBox1.Text += result;
                    num1 = result;
                    ch = "/";
                    break;
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {

            if (ch != "+")
            {
                num1 = double.Parse(textBox1.Text)+ num1;
            }
            else
            {
                num1 = Convert.ToInt32(textBox1.Text);
                ch = "";
            }
            textBox1.Text = "";
            op = "+";

        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            if (ch != "-")
            {
                num1 =  num1 - double.Parse(textBox1.Text);
            }
            else
            {
                num1= Convert.ToInt32(textBox1.Text);
                ch = "";
            }

            textBox1.Text = "";

            op = "-";

            textBox1.Text += op;

        }

        private void btnMul_Click(object sender, EventArgs e)
        {

            if (ch != "*")
            {
            num1 = double.Parse(textBox1.Text) * num1;
            }
            else
            {
                num1 = Convert.ToInt32(textBox1.Text);
                ch = "";
            }
            textBox1.Text = "";
            op = "*";

        }

        private void btnDiv_Click(object sender, EventArgs e)
        {

            if (ch != "/") 
            {
            num1 = double.Parse(textBox1.Text) / num1;
            }
            else
            {
                num1 = Convert.ToInt32(textBox1.Text);
                ch = "";
            }
            textBox1.Text = "";
            op = "/";


        }

首先,由于您的方法
button1\u单击\u 1
button10\u单击
都执行相同的操作,因此您可以用一个方法替换它们:

private void NumberButtonClicked(object sender, EventArgs e)
{
    var buttonClicked = (Button)sender;
    if (textBox1.Text == "+" || textBox1.Text == "-" || textBox1.Text == "*" || textBox1.Text == "/")
    {
        textBox1.Clear();
        textBox1.Text = textBox1.Text + buttonClicked.Text;
    }
    else
        textBox1.Text = textBox1.Text + buttonClicked.Text;
}
然后关于您的错误:在
btnSub_中单击
检查字段
ch
,但您从未将其设置为值(除了空字符串
ch=”“
)。。。也许你真的想检查
op
?我不确定

另一件事是使用
btnDiv\u单击
方法。您可以将输入的值除以之前输入的值。。。难道不是相反吗

num1 = num1 / double.Parse(textBox1.Text);

最好的办法是,您检查这些内容,如果它仍然不起作用,您可以在
btnSub\u单击的开始处设置一个断点,在单击时检查变量的值,并通过单步执行来调试方法。

问题可能在于按“等于”后没有清除这些值。让我们一步一步地看一下您的示例(7-4-3):

按equals键后不会清除值,因此后续操作将受上一个num1值的影响。这不是计算器通常的工作方式,所以我猜这不是您想要的行为,可能是一个在后续操作中导致奇怪结果的错误


如果第一次操作的结果正确,但之后不正确,这就是原因。

ch is string,则可能需要使用
.Equal()
方法。并且,您没有将文本框文本设置为Substraction的结果
textbox1.Text=num1.ToString()
我想它只是显示为输出?当您显示要显示的值时,通常效果会更好。您将其重置为“”,然后添加
-
。它显示错误的输出7-4-3我给它显示8尝试提供完整的(至少对于这种方法)代码,如ch和num1…您给出
7-4-3
是什么意思如果您在textbox1中插入,那么您的代码将在第
行抛出异常。Parse(textbox1.Text)
1) press 7:
textbox1.Text="7";

2) press -:
num1 =  num1 - double.Parse(textBox1.Text); = 7
ch = "";

3) press 4:
textbox1.Text="4";

4) press -:
num1 =  num1 - double.Parse(textBox1.Text); = 7-4 = 3
ch="";

5) press 3:
textbox1.Text="3";

6) press equals?
num2 = 3;
result = num1 - num2; = 3-3 = 0
textBox1.Text += result; = "0", which is correct
num1 = result; = 0 (accidentaly in this case, but sometimes another value will be left in here)