Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

Making my first calculator in C# and I'm stuck

Just getting into C# and programming in general, i'm making a calculator with the windows form app project template. i'm trying to make my calculator work like a regular one does where if you press 1+1 and then + again it shows the sum of the 1+1 and waits for the next number input to add to that.

My code for hitting any of the number buttons is tb1.Text = tb1.Text + "the number being pressed"

I have defined 3 variables so far int num1 = 0; int num2 = 0; int result;

my code for the onclick of the "+" button is as follows

        private void button11_Click(object sender, EventArgs e)
        {
            //Add
            if (num1 == 0)
            {
                num1 = Convert.ToInt32(tb1.Text);
                tb1.Clear();
            }
            else
            {
                num2 = Convert.ToInt32(tb1.Text);
                result = num1 + num2;
                tb1.Text = Convert.ToString(result);
                num1 = result;
                tb1.Clear();
                tb2.Text = Convert.ToString(num1);
                tb3.Text = Convert.ToString(num2);
            }
        } 

this code works and i can see it working because i have debugging text boxes(tb2 and tb3) and tb2 shows me that num1 is = to the sum of the numbers i am adding together. i am able to add two numbers and then add another number to that, so on. but id like the sum of the first two numbers to be displayed in tb1 when i hit the + sign again to add another number to it. i figured i would do that by adding

tb1.Text = Convert.ToString(num1);

before my if statement (right below the //Add comment) in the addition button click method. this instead makes nothing show up in the debug tb2 textbox and doesn't fix my problem and im unsure why. even if i parse it back to an int right after this new line nothing changes. if more or clearer information is needed, i will oblige


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To explain the this instead makes nothing show up in the debug tb2 textbox and doesn't fix my problem and im unsure why:

When you add tb1.Text = Convert.ToString(num1); before the first if statement, you end up with the following flow:

  1. Program loads. num1 is 0, tb1.Text is empty
  2. User enters 5 in tb1
  3. button1_Click is called
    1. tb1.Text is set to num1 (0)
    2. Enter if (num1 == 0) block
    3. num1 is set to tb1.Text (0)
  4. Repeat steps 2 and 3

With this, you end up endlessly setting num1 to 0 and never get to the else block to set tb2.Text.


Rather than resetting the tb1.Text value at the beginning of button11_Click, you might just need to remove the tb1.Clear(); in the else block. That statement doesn't make much sense considering it's being called just after tb1.Text = Convert.ToString(result);. So the code would end up being:

//Add
if (num1 == 0)
{
    num1 = Convert.ToInt32(tb1.Text);
    tb1.Clear();
}
else
{
    num2 = Convert.ToInt32(tb1.Text);
    result = num1 + num2;
    tb1.Text = Convert.ToString(result);
    num1 = result;
    tb2.Text = Convert.ToString(num1);
    tb3.Text = Convert.ToString(num2);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...