如何在WPF计算器中修复结果

时间:2019-05-12 12:29:45

标签: c# wpf

我正在使用WPF制作计算器,并且WPF计算器仅在第一次计算时才能完美运行。如果要在此之后进行其他计算,它将不起作用。

我尝试使用Equals_Click中的if语句,我说过如果按下等于按钮2次,则应该将number1和number2设置为0,这样我就可以再次进行计算。即使我实现了这一点,我也没有摆脱这个问题。

public partial class MainWindow : Window
{
    // Variables so we can calulate
    long number1 = 0;
    long number2 = 0;
    string operation = "";

    public MainWindow()
    {
        InitializeComponent();
    }

    private void One_Click(object sender, RoutedEventArgs e)
    {
        // If the operation isn't empty that means we clicked number1 so we need to click number2

        if (operation == "")
        {
            number1 = (number1 * 10) + 1;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 1;
            Display.Text = number2.ToString();
        }
    }

    private void Two_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 2;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 2;
            Display.Text = number2.ToString();
        }
    }

    private void Three_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 3;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 3;
            Display.Text = number2.ToString();
        }
    }

    private void Four_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 4;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 4;
            Display.Text = number2.ToString();
        }
    }

    private void Five_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 5;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 5;
            Display.Text = number2.ToString();
        }
    }

    private void Six_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 6;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 6;
            Display.Text = number2.ToString();
        }
    }

    private void Seven_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 7;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 7;
            Display.Text = number2.ToString();
        }
    }

    private void Eight_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 8;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 8;
            Display.Text = number2.ToString();
        }
    }

    private void Nine_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10) + 9;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10) + 9;
            Display.Text = number2.ToString();
        }
    }

    private void Zero_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = (number1 * 10);
            Display.Text = number1.ToString();
        }
        else
        {
            number2 = (number2 * 10);
            Display.Text = number2.ToString();
        }
    }

    private void Plus_Click(object sender, RoutedEventArgs e)
    {
        operation = "+";
        Display.Text = "0";
    }

    private void Minus_Click(object sender, RoutedEventArgs e)
    {
        operation = "-";
        Display.Text = "0";
    }

    private void Multiple_Click(object sender, RoutedEventArgs e)
    {
        operation = "*";
        Display.Text = "0";
    }

    private void Divide_Click(object sender, RoutedEventArgs e)
    {
        operation = "/";
        Display.Text = "0";
    }

    private void Equals_Click(object sender, RoutedEventArgs e)
    {
        switch (operation)
        {
            case "+":
                Display.Text = (number1 + number2).ToString();
                break;
            case "*":
                Display.Text = (number1 * number2).ToString();
                break;
            case "-":
                Display.Text = (number1 - number2).ToString();
                break;
            case "/":
                Display.Text = (number1 / number2).ToString();
                break;
        }
    }
    private void ClearEntry_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 = 0;
        }
        else
        {
            number2 = 0;
        }
        Display.Text = "0";
    }
    private void Clear_Click(object sender, RoutedEventArgs e)
    {
        number1 = 0;
        number2 = 0;
        operation = "";
        Display.Text = "0";
    }

    private void PlusMinus_Click(object sender, RoutedEventArgs e)
    {
        if (operation == "")
        {
            number1 *= -1;
            Display.Text = number1.ToString();
        }
        else
        {
            number2 *= -1;
            Display.Text = number2.ToString();
        }
    }
}

很抱歉张贴整个程序,我这样做是为了让您有更多关于此问题的信息。

在第一次尝试中,如果我们计算5 + 7,则输出为10。 在第二次尝试中,如果我们清除然后按数字3,则输出将为53而不是3。

0 个答案:

没有答案