简体   繁体   English

C#计算器在某些操作Windows窗体应用程序中使用列表数组错误

[英]C# Calculator using List Array error at some operations Windows Form Application

Hello I'm still new to programming and yes this is not the best code you will see... I tried making calculator on C# windows form for fun and I'm having trouble on the subtraction and division operations, but the addition and multiplication works perfectly fine for me. 您好,我仍然是编程的新手,是的,这不是您将看到的最好的代码...我尝试在C#Windows窗体上制作计算器很有趣,但在减法和除法运算上遇到了麻烦,但是加法和乘法对我来说效果很好。 I decided to have a list array so that I would be able to input numbers as much as I want. 我决定有一个列表数组,这样我就可以输入任意数量的数字。

The error for the subtraction is when I input for example 5 - 2 the result will be -3 减法的错误是当我输入例如5-2时,结果将是-3

As for the division the error is that the result is always 1 至于除法错误是结果总是1

Please tell me where did I go wrong and give a detailed explanation if possible so that I would understand more about programming. 请告诉我我哪里出错了,并在可能的情况下给出详细的解释,以便我对编程有所了解。 Thanks in advance! 提前致谢!

namespace CalculatorTestForm1
{
public partial class Form1 : Form
{
    public static List<int> Numlist = new List<int>();
    public static string operation;

    public Form1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, EventArgs e)
    {
        Button Num = (Button)sender;
        TXTBox.Text += Num.Text;
    }

    private void BPlus_Click(object sender, EventArgs e)
    {
        operation = "add";
        int AddNum = Convert.ToInt32(this.TXTBox.Text);
        Numlist.Add(AddNum);
        TXTBox.Text = "";          
    }

    private void BEquals_Click(object sender, EventArgs e)
    {       
        int AddNum = Convert.ToInt32(this.TXTBox.Text);
        Numlist.Add(AddNum);

        int sum = 0;
        int product = 1;
        int quotient = 1;
        int difference = 0;

        if (operation == "add"){
            foreach (int value in Numlist)
            {
                sum += value;
            }
            string Answer = sum.ToString();
            TXTBox.Text = Answer;    
        }else if(operation == "minus"){
            foreach (int value in Numlist)
            {
                difference = value - difference;
            }
            string Answer = difference.ToString();
            TXTBox.Text = Answer;
        }
        else if (operation == "multiply")
        {
            foreach (int value in Numlist)
            {
                product *= value;
            }
            string Answer = product.ToString();
            TXTBox.Text = Answer;
        }
        else if (operation == "divide")
        {
            foreach (int value in Numlist)
            {
                quotient = value / value;
            }
            string Answer = quotient.ToString();
            TXTBox.Text = Answer;
        }
        Numlist.Clear();
    }

    private void BClear_Click(object sender, EventArgs e)
    {
        TXTBox.Text = "";
        Numlist.Clear();
    }

    private void BMinus_Click(object sender, EventArgs e)
    {
        operation = "minus";
        int AddNum = Convert.ToInt32(this.TXTBox.Text);
        Numlist.Add(AddNum);
        TXTBox.Text = "";         
    }

    private void BDivide_Click(object sender, EventArgs e)
    {
        operation = "divide";
        int AddNum = Convert.ToInt32(this.TXTBox.Text);
        Numlist.Add(AddNum);
        TXTBox.Text = "";  
    }

    private void BMulti_Click(object sender, EventArgs e)
    {
        operation = "multiply";
        int AddNum = Convert.ToInt32(this.TXTBox.Text);
        Numlist.Add(AddNum);
        TXTBox.Text = "";         
    }
  }
}

For the division it's obvious: 对于该部门,很明显:

quotient = value / value;

value / value will always be 1 . value / value将始终为1

There must be quotient in that loop somewhere... 该循环中某处一定有quotient ...

For the subtraction the problem is that because of the way you do it, the order of the numbers are reversed. 对于减法,问题在于由于您的处理方式,数字的顺序相反。

lets say 5 - 2: 假设5-2:

foreach (int value in Numlist)
{
    difference = value - difference;
}

NumList = {5,2}

1st iteration: difference = value(5) - difference(0) = 5 第一次迭代: difference = value(5) - difference(0) = 5

2nd iteration: difference = value(2) - difference(5) = -3 第二次迭代: difference = value(2) - difference(5) = -3

You should reverse the order of the loop: NumList.Reverse() 您应该反转循环的顺序: NumList.Reverse()

And for the division as well: 对于部门也是如此:

Division: 师:

foreach (int value in Numlist.Reverse())
{
    quotient = value / quotient;
}

Subtraction: 减法:

foreach (int value in Numlist)
{
    difference = value - difference;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM