尝试同时减去 2 个值会导致它们相加,除加法之外的所有运算符也不起作用

Trying to minus 2 values together results in them adding instead, also all operators exept addition do not function

出于某种原因,当我测试代码时,minus if 语句将两个值加在一起而不是减去它们,以及另一个 if 语句用于时间和减法 return 错误消息

System.FormatException: 'Input string was not in a correct format.'

尽管如此,第一个 if 加法语句仍然工作正常

    public Form1()
    {
        InitializeComponent();
    }

    private void Calculation()
    {
        decimal answer;

        if (savedOperator == "+")
        {
            answer = savedNumber + Convert.ToDecimal(txtBox.Text);
            txtBox.Text = answer.ToString();
            savedOperator = "";
        }

        if (savedOperator == "-")
        {
            answer = savedNumber - Convert.ToDecimal(txtBox.Text);
            txtBox.Text = answer.ToString();
            savedOperator = "";
        }

        if (savedOperator == "*")
        {
            answer = savedNumber * Convert.ToDecimal(txtBox.Text);
            txtBox.Text = answer.ToString();
            savedOperator = "";
        }

        if (savedOperator == "/")
        {
            answer = savedNumber / Convert.ToDecimal(txtBox.Text);
            txtBox.Text = answer.ToString();
            savedOperator = "";
        }

    }

    private void SaveNumber()
    {
        savedNumber = System.Convert.ToDecimal(txtBox.Text);

        txtUpperBox.Text = savedNumber.ToString();
    }

    // numbers

    private void btn1_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "1";
    }

    private void btn2_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "2";
    }

    private void btn3_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "3";
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "4";
    }

    private void btn5_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "5";
    }

    private void btn6_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "6";
    }

    private void btn7_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "7";
    }

    private void btn8_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "8";
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "9";
    }

    private void btn0_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + "0";
    }

    // operators

    private void btnPlus_Click(object sender, EventArgs e)
    {
        SaveNumber();
        txtBox.Clear();
        savedOperator = "+";
        txtBox.Text = "+";
    }

    private void btnMinus_Click(object sender, EventArgs e)
    {
        SaveNumber();
        txtBox.Clear();
        savedOperator = "-";
        txtBox.Text = "-";
    }

    private void btnTimes_Click(object sender, EventArgs e)
    {
        SaveNumber();
        txtBox.Clear();
        savedOperator = "*";
        txtBox.Text = "*";
    }

    private void btnDivide_Click(object sender, EventArgs e)
    {
        SaveNumber();
        txtBox.Clear();
        savedOperator = "/";
        txtBox.Text = "/";
    }

    private void btnEquals_Click(object sender, EventArgs e)
    {
        Calculation();
    }
}

}

我没有发现你的数学逻辑有任何明显的问题。

我在 https://dotnetfiddle.net/Uo2EHM 测试过它,它工作正常。问题可能出在您获得的输入或转换中。

您可以在代码中的不同点设置断点以查看值是什么,或者将值打印到另一个“调试”文本框。

我看到的一些潜在问题是数字和字符串之间的转换。有时,它可能不会给您预期的结果。

此外,您可能希望使用 switch 语句而不是多个 if 语句,但这不是您看到的错误。

问题可能出在执行计算之前的 TextBox 值上吗?如果您有“8--3”,它将与 8 - (-3) 相同。尝试使用不同的文本框输入代码和运算符,看看是否出现问题。