C# 计算器,等于按钮重复操作无法正常工作

C# Calculator, Equals button repeat operation not working properly

我正在构建一个简单的 C# 计算器,但我在使用等号按钮时遇到了一些问题。第一次单击以计算指定的方程式工作正常,但我希望每次用户持续单击等于按钮时使用第二个数字(在本例中为 y)重复该操作。目前,该操作是重复操作的总数,而不是原来的秒数。任何帮助将不胜感激! 这是 class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace BMW_CALC_UI
   {
    public class Calculator
    {
        public double Add(double x, double y)
        {
            // Addition
            return x + y;
        }

        public double Subtract(double x, double y)
        {
            // Subtraction
            return x - y;
        }

        public double Multiply(double x, double y)
        {
            // Multiplication
            return x * y;
        }

        public double Divide(double x, double y)
        {
            // Division
            return x / y;
        }

        public double SquareRoot(double x)
        {
            // Square Root (must be called as a double)
            return Math.Sqrt(x);
        }

        public double Reciprocal(double x)
        {
            // Reciprocal
            return 1 / x;
        }

        public double ChangeSign(double x)
        {
            // Change sign
            return -x;
        }
    }
    }

这是表格:

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

namespace BMW_CALC_UI
{
public partial class frmCalculator : Form
{
    public frmCalculator()
    {
        InitializeComponent();
    }

    // Set variables
    double x;
    double y;
    char operation;


    private void btnEquals_Click(object sender, EventArgs e)
    {
        if (txtDisplay.Text != "")
        {


            // Instantiate the instance
            Calculator myCalculator = new Calculator();

            // Store second number
           if (double.TryParse(txtDisplay.Text, out y))

            switch (operation)
            {
                case '+':
                    // Add addition method
                    txtDisplay.Text = (myCalculator.Add(x, y)).ToString();
                    break;

                case '-':
                    // Add subtraction method
                    txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString();
                    break;

                case '*':
                    // Add multiplication method
                    txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString();
                    break;

                case '/':
                    if (y == 0)
                    {
                        // Display error message
                        txtDisplay.Text = "Cannot divide by zero";
                        return;
                    }
                    // Add division method
                    txtDisplay.Text = (myCalculator.Divide(x, y)).ToString();
                    break;
            }

            // Reset 
            x = y;
        }                        

    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        // Clear all data
        txtDisplay.Clear();
        x = 0;
        y = 0;
    }

    private void btnBack_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

            // Clear last number entered
            if (txtDisplay.Text != "" && txtDisplay.TextLength > 0)
            {
                txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.TextLength - 1);
            }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '+';
            txtDisplay.Clear();
        }
    }

    private void btnSubtract_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '-';
            txtDisplay.Clear();
        }
    }

    private void btnMultiply_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '*';
            txtDisplay.Clear();
        }
    }

    private void btnDivide_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
        operation = '/';
        txtDisplay.Clear();
        }
    }

    private void btnSquareRoot_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform square root operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.SquareRoot(x)).ToString();
        }
    }

    private void btnReciprocal_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform reciprocal operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.Reciprocal(x)).ToString();
        }
    }

    private void btnSign_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform sign operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.ChangeSign(x)).ToString();
        }
    }

    private void btnDecimalPoint_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Add decimal point if none exist
        if (txtDisplay.Text.Contains("."))
        {
            txtDisplay.Text = txtDisplay.Text;
        }
        else
        {
            txtDisplay.Text = txtDisplay.Text + ".";
        }
    }

    private void btnZero_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // If textbox starts doesn't start with 0 or Textbox
        // contains a decimal point then it is ok to add a zero
        else if (!txtDisplay.Text.StartsWith("0") || txtDisplay.Text.Contains("."))
        {
            // Add 0 to display
            txtDisplay.Text += "0";
        }
    }

    private void btnOne_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 1 to display
        txtDisplay.Text += "1";
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 4 to display
        txtDisplay.Text += "4";
    }

    private void btnSeven_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 7 to display
        txtDisplay.Text += "7";
    }

    private void btnTwo_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 2 to display
        txtDisplay.Text += "2";
    }

    private void btnFive_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 5 to display
        txtDisplay.Text += "5";
    }

    private void btnEight_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 8 to display
        txtDisplay.Text += "8";
    }

    private void btnThree_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 3 to display
        txtDisplay.Text += "3";
    }

    private void btnSix_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 6 to display
        txtDisplay.Text += "6";
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 9 to display
        txtDisplay.Text += "9";
    }   
}
}

您从存储输出的同一文本框中获取第二个数字

这是第二个: if (double.TryParse(txtDisplay.Text, out y))

您在此处存储输出: txtDisplay.Text = (myCalculator.Add(x, y)).ToString();

只需将结果存储在其他地方,例如您可以在定义 x,y 的地方定义 "result"。 将结果存储在那里。

并在"equals"函数中写入:

case '+':
// Add addition method
txtDisplay.Text = (myCalculator.Add(result, y)).ToString();
break;

当然如果这是你第一次添加数字,你需要知道它(存储在一些布尔值中)

感谢您的帮助,下面的代码是我最终得到的,效果很好。

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

namespace BMW_CALC_UI
{
public partial class frmCalculator : Form
{
    public frmCalculator()
    {
        InitializeComponent();
    }

    // Set variables
    double x;
    double y;
    char operation;
    bool EqualsRepeated = false;

    private void btnEquals_Click(object sender, EventArgs e)
    {

        if (txtDisplay.Text != "")
        {

            // Instantiate the instance
            Calculator myCalculator = new Calculator();

            // Store second number
            if (EqualsRepeated == false)
            {
                if (double.TryParse(txtDisplay.Text, out y))

                    switch (operation)
                    {
                        case '+':
                            // Add addition method
                            txtDisplay.Text = (myCalculator.Add(x, y)).ToString();
                            break;

                        case '-':
                            // Add subtraction method
                            txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString();
                            break;

                        case '*':
                            // Add multiplication method
                            txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString();
                            break;

                        case '/':
                            if (y == 0)
                            {
                                // Display error message
                                txtDisplay.Text = "Cannot divide by zero";
                                return;
                            }
                            // Add division method
                            txtDisplay.Text = (myCalculator.Divide(x, y)).ToString();
                            break;                   
                    }
                // Set button clicked to true
                EqualsRepeated = true;               
            }

            else
            {
                // If equals has already been clicked
                if (EqualsRepeated == true)

                    if (double.TryParse(txtDisplay.Text, out x))

                    switch (operation)
                    {
                        case '+':
                            // Add addition method
                            txtDisplay.Text = (myCalculator.Add(x, y)).ToString();
                            break;

                        case '-':
                            // Add subtraction method
                            txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString();
                            break;

                        case '*':
                            // Add multiplication method
                            txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString();
                            break;

                        case '/':
                            if (y == 0)
                            {
                                // Display error message
                                txtDisplay.Text = "Cannot divide by zero";
                                return;
                            }
                            // Add division method
                            txtDisplay.Text = (myCalculator.Divide(x, y)).ToString();
                            break;
                    }
            }

        }                        

    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        // Clear all data
        txtDisplay.Clear();
        EqualsRepeated = false;
        x = 0;
        y = 0;
    }

    private void btnBack_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

            // Clear last number entered
            if (txtDisplay.Text != "" && txtDisplay.TextLength > 0)
            {
                txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.TextLength - 1);
            }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '+';
            txtDisplay.Clear();
        }
    }

    private void btnSubtract_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '-';
            txtDisplay.Clear();
        }
    }

    private void btnMultiply_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '*';
            txtDisplay.Clear();
        }
    }

    private void btnDivide_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
        operation = '/';
        txtDisplay.Clear();
        }
    }

    private void btnSquareRoot_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform square root operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.SquareRoot(x)).ToString();
        }
    }

    private void btnReciprocal_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform reciprocal operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.Reciprocal(x)).ToString();
        }
    }

    private void btnSign_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform sign operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.ChangeSign(x)).ToString();
        }
    }

    private void btnDecimalPoint_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Add decimal point if none exist
        if (txtDisplay.Text.Contains("."))
        {
            txtDisplay.Text = txtDisplay.Text;
        }
        else
        {
            txtDisplay.Text = txtDisplay.Text + ".";
        }
    }

    private void btnZero_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // If textbox starts doesn't start with 0 or Textbox
        // contains a decimal point then it is ok to add a zero
        else if (!txtDisplay.Text.StartsWith("0") || txtDisplay.Text.Contains("."))
        {
            // Add 0 to display
            txtDisplay.Text += "0";
        }
    }

    private void btnOne_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 1 to display
        txtDisplay.Text += "1";
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 4 to display
        txtDisplay.Text += "4";
    }

    private void btnSeven_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 7 to display
        txtDisplay.Text += "7";
    }

    private void btnTwo_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 2 to display
        txtDisplay.Text += "2";
    }

    private void btnFive_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 5 to display
        txtDisplay.Text += "5";
    }

    private void btnEight_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 8 to display
        txtDisplay.Text += "8";
    }

    private void btnThree_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 3 to display
        txtDisplay.Text += "3";
    }

    private void btnSix_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 6 to display
        txtDisplay.Text += "6";
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 9 to display
        txtDisplay.Text += "9";
    }   
 }
 }