我制作的 C# 计算器给出了无意义的结果

C# Calculator I made is giving nonsense results

当我给计算器输入:1+11= 它给出的答案是 14。如果我对行 "total=num1+num2;" 进行表述,我会得到正确的结果,但是当我对行进行表述时:"total+=num1+num2;" 它给了我 14,我不知道为什么会这样。这意味着我不能按照我想要的方式使用总数,因为我必须覆盖它并且不能有 运行 总数。 :-/

`using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        public double num1;
        public double num2;
        public double total;
        public string activestring;
        public int operand;
        public bool Isnum1;
        public bool Isnum2;
        public Form1()
        {
            InitializeComponent();
        }

        //a #1 button on a calculator.
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += 1;
            activestring += 1;

            if (Isnum1)
            {
                num2 = double.Parse(activestring);
                if (operand == 1)
                {
                    total += num1 + num2;
                }
            }
        }
        // a + button on a calculator.
        private void buttonPlus_Click(object sender, EventArgs e)
        {
            if (!Isnum1)
            {
                operand = 1;
                num1 = double.Parse(activestring);
                activestring = "";
                Isnum1 = true;
                textBox1.Text += "+";
            }
        }

        // an = button on a calculator.
        private void buttonEQ_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox1.Text = total.ToString();
        }
    }
}`

我同意@bolov 的观点,调试你的程序在这里是一个很好的学习练习,但如果你正在苦苦挣扎,并且出于学术目的学习如何改进你所拥有的而不是别人告诉你完全不同的方式,下面是怎么回事:

你的程序正在做的是(只关注导致错误的位):

单击按钮“1”会设置以下内容:
- activestring = "1"
单击按钮“+”,设置以下内容:
- num1 = 1
- activestring = ""
- isnum1 = true;
单击按钮“1”会设置以下内容:
- activestring = "1"
- num2 = 1
- 总计 = 2(总计(0)+ num1(1)+ num2(1))
再次单击按钮“1”,设置以下内容:
- activestring = "11"
- num2 = 11
- 总计 = 14(总计(2)+ num1(1)+ num2(11))

这是尝试累积仅部分写入的数字的结果。
有很多可怕的方法可以在“1”按钮单击代码中解决这个问题(例如,在更新 activestring 之前和之后进行解析,然后取下以前的值并添加新值),但感觉就像在下面的代码示例中使用 2 个总数将以更简洁的方式实现您的目标,而不会偏离您设置的结构太远:

(我还对其进行了调整以支持继续添加(多次调用“+”),因为这似乎是您最终的目标)。

(免责声明:我实际上 运行 根本没有通过编译器完成此操作,但它应该有助于让您走上正确的轨道)。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        public double num1;
        public double num2;
        public double total;
        public string activestring;
        public int operand;
        public bool Isnum1;  
        public bool IsPrevCharNumeric;  // New

        public Form1()
        {
            InitializeComponent();
        }

        //a #1 button on a calculator.
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += 1;
            activestring += 1;

            if (Isnum1)
            {
                num2 = double.Parse(activestring);
                if (operand == 1)
                {
                    total = num1 + num2;  // Changed
                }
                isPrevCharNumeric = true;  // New
            }
        }
        // a + button on a calculator.
        private void buttonPlus_Click(object sender, EventArgs e)
        {
            if (IsPrevCharNumeric)  // Changed
            {
                operand = 1;
                total = num1 + double.Parse(activestring);  // Changed
                num1 = total;                               // Changed
                activestring = "";
                Isnum1 = true;
                IsPrevCharNumeric = false;                  // New
                textBox1.Text += "+";
            }
        }

        // an = button on a calculator.
        private void buttonEQ_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox1.Text = total.ToString();
        }
    }
}