简单计算器(C#)-如何获得涉及textBox2中两个整数的计算结果?

时间:2019-01-24 18:27:20

标签: c#

我还是C#编程语言的新手。我必须创建一个简单的计算器,该计算器可以对用户选择的两个数字进行加,减,乘,除运算,而无需小数。我的计算器有两个输入文本框和一个输出文本框。我只有4个操作员按钮。 +,-,*,/。此外,我还添加了“退格”按钮。

当用户输入两个值时,必须按下操作员按钮之一。例如;如果用户在“ textBox1”中键入10,在“ textBox2”中键入20,然后按“ Button15”,则总和的值将显示在“ textBox2”中。

代码下方有图像的链接。

所以我的问题出在“ =” Button15下的代码-我能够显示我的用户输入值,但结果有问题。我使用了自己的方法,可能不正确。

我将不胜感激。

这是源代码:

public partial class Form1 : Form
{
    int FirstNumber;
    string Operation;
    string l;
    public Form1()
    {
        InitializeComponent();
    }


    private void button4_Click(object sender, EventArgs e)
    {
        if(textBox1.Text=="0")
        {
            textBox1.Text = "6";
        }
        else
        {
            textBox1.Text = textBox1.Text + "6";
        }
    }

    private void button15_Click(object sender, EventArgs e)
    {

        int SecondNumber;
        int Result;

        if (Operation == "+")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("+");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            Result = (FirstNumber + SecondNumber);
            textBox2.Text = Convert.ToString(Result);


        }
        else if (Operation == "-")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("-");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            Result = (FirstNumber - SecondNumber);
            textBox2.Text = Result.ToString(); 

        }
        else if (Operation == "x")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("x");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            Result = (FirstNumber * SecondNumber);
            textBox2.Text = Convert.ToString(Result);

        }
        else if(Operation=="/")
        {
            string l = textBox1.Text;
            int index = l.IndexOf("/");
            string result = l.Substring(index + 1);
            SecondNumber = Convert.ToInt16(result);
            if ( SecondNumber == 0)
            {
                textBox1.Text = "Cannot divide by 0";
            }
            else
            {
                Result = (FirstNumber / SecondNumber);
                textBox2.Text = Convert.ToString(Result);
            }
        }



    }

    private void button1_Click(object sender, EventArgs e)      
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "1";
        }
        else
        {
            textBox1.Text = textBox1.Text + "1";
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "2";
        }
        else
        {
            textBox1.Text = textBox1.Text + "2";
        }

    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "3";
        }
        else
        {
            textBox1.Text = textBox1.Text + "3";
        }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "5";
        }
        else
        {
            textBox1.Text = textBox1.Text + "5";
        }
    }

    private void button6_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "4";
        }
        else
        {
            textBox1.Text = textBox1.Text + "4";
        }
    }

    private void button9_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "7";
        }
        else
        {
            textBox1.Text = textBox1.Text + "7";
        }
    }

    private void button8_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "8";
        }
        else
        {
            textBox1.Text = textBox1.Text + "8";
        }
    }

    private void button7_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "0")
        {
            textBox1.Text = "9";
        }
        else
        {
            textBox1.Text = textBox1.Text + "9";
        }
    }

    private void button10_Click(object sender, EventArgs e)
    {

            textBox1.Text = textBox1.Text + "0";

    }

    private void button11_Click(object sender, EventArgs e)
    {

       int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "-";

        textBox1.Text = textBox1.Text + Operation;


    }

    private void button12_Click(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "+";
        textBox1.Text = textBox1.Text + Operation;


    }

    private void button13_Click(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "x";
        textBox1.Text = textBox1.Text + Operation;


    }

    private void button14_Click(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        string Operation = "/";
        textBox1.Text = textBox1.Text + Operation;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int FirstNumber = Convert.ToInt32(textBox1.Text);
        textBox1.Text = textBox1.Text + "0";
    }


    private void button16_Click(object sender, EventArgs e)
    {
        textBox1.Text = "0";
    }

Here is the image of my calculator. The textBox1, the one at the top is happily displaying the user input but the one below, textBox2 is not working when clicked on "="

2 个答案:

答案 0 :(得分:2)

在按钮事件处理程序中,您声明新的FirstNumberOperation变量并进行分配。这些与表单的FirstNumberOperation不同。您无需再次声明它们,只需分配给它们即可。

private void button11_Click(object sender, EventArgs e)
{

   int FirstNumber = Convert.ToInt32(textBox1.Text);
    string Operation = "-";

    textBox1.Text = textBox1.Text + Operation;


}

应该是

private void button11_Click(object sender, EventArgs e)
{

    FirstNumber = Convert.ToInt32(textBox1.Text);
    Operation = "-";

    textBox1.Text = textBox1.Text + Operation;
} 

和其余按钮单击事件处理程序类似。

答案 1 :(得分:1)

首先,请给控件和事件处理程序赋予有意义的名称。您可以在设计视图的属性列中更改名称。

第二,您在处理操作员按钮点击的方法中声明新变量。 例如,事件处理程序中有int FirstNumberstring Operation。在声明之后,具有相同名称的类变量将被遮盖,并且您只能在这些处理程序中访问新变量。

删除intstring。拥有它的方式,您永远不会更改类字段。