减法在c#中不起作用

时间:2015-02-19 05:50:07

标签: c# .net subtraction

嗨我在c#上做程序.i写了减去三个数字但输出错误的逻辑。这是我的代码。任何帮助都将不胜感激。

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 = "/";


        }

2 个答案:

答案 0 :(得分:0)

首先,由于您的方法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的开头设置一个断点,检查你的变量点击它并调试时的值通过单步执行的方法。

答案 1 :(得分:0)

问题可能是在按下“等于”后没有清除值。让我们一步一步地通过你的例子(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)

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

如果您在第一次操作时获得了正确的结果,但之后不正确,这应该是原因。

相关问题