c中的减法运算出错

本文关键字:运算 出错 | 更新日期: 2024-06-14 03:35:52

我正在用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 = "/";

        }

c中的减法运算出错

首先,由于方法button1_Click_1button10_Click都做相同的事情,因此可以用一个方法来替换它们:

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_Click中,您检查字段ch,但从未将其设置为值(除了空字符串ch = "")。。。也许你真的想检查op?我不确定。。。

另一件事是使用btnDiv_Click方法。在这里,您可以将输入的值除以之前输入的值。。。不是应该反过来吗?

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

最好的办法是,你检查这些东西,如果它仍然不起作用,你可以在btnSub_Click的开头设置一个断点,当你点击它时检查你的变量有什么值,并通过逐步遍历来调试方法

问题可能是按下"equals"后没有清除值。让我们一步一步地看你的例子(7-4-3):

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)

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

如果你第一次手术的结果是正确的,但之后又不正确,这就是为什么。