-2

我的计算器代码有问题。加号和减号按预期工作,但是除法和乘法每次都得到零的答案。这是代码:

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

namespace Calculator
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btn1_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn1.Text;
    }

    private void btn2_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn2.Text;
    }

    private void btn3_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn3.Text;
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn4.Text;
    }

    private void btn5_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn5.Text;
    }

    private void btn6_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn6.Text;
    }

    private void btn7_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn7.Text;
    }

    private void btn8_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn8.Text;
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn9.Text;
    }

    private void btn0_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btn0.Text;
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtDisplay.Clear();
    }

    Double total1 = 0;
    Double total2 = 0;
    bool minusButtonClicked = false;
    bool plusButtonClicked = false;
    bool divideButtonClicked = false;
    bool multiplyButtonClicked = false;

    private void btnPlus_Click(object sender, EventArgs e)
    {
        total1 = total1 + Double.Parse(txtDisplay.Text);
        txtDisplay.Clear();

        plusButtonClicked = true;
        minusButtonClicked = false;
        divideButtonClicked = false;
        multiplyButtonClicked = false;
    }

    private void btnMinus_Click(object sender, EventArgs e)
    {
        total1 = total1 - Double.Parse(txtDisplay.Text);
        txtDisplay.Clear();

        plusButtonClicked = false;
        minusButtonClicked = true;
        divideButtonClicked = false;
        multiplyButtonClicked = false;
    }

    private void btnDivide_Click(object sender, EventArgs e)
    {
        total1 = total1 / Double.Parse(txtDisplay.Text);
        txtDisplay.Clear();

        plusButtonClicked = false;
        minusButtonClicked = false;
        divideButtonClicked = true;
        multiplyButtonClicked = false;
    }

    private void btnMultiply_Click(object sender, EventArgs e)
    {
        total1 = total1 * Double.Parse(txtDisplay.Text);
        txtDisplay.Clear();

        plusButtonClicked = false;
        minusButtonClicked = false;
        divideButtonClicked = false;
        multiplyButtonClicked = true;
    }

    private void btnEquals_Click(object sender, EventArgs e)
    {
        if (plusButtonClicked == true)
        {
            total2 = total1 + Double.Parse(txtDisplay.Text);
        }
        else if (minusButtonClicked == true)
        {
            total2 = total1 - Double.Parse(txtDisplay.Text);
        }
        else if (divideButtonClicked == true)
        {
            total2 = total1 / Double.Parse(txtDisplay.Text);
        }
        else if (multiplyButtonClicked == true)
        {
            total2 = total1 * Double.Parse(txtDisplay.Text);
        }

        txtDisplay.Text = total2.ToString();
        total1 = 0;
    }

}
}

是 if else 语句中的内容还是其他常见错误?

4

2 回答 2

3

您将 total1 和 total2 初始化为 0。

你的代码:

total1 = total1 * Double.Parse(txtDisplay.Text);

总是给你 0 因为 0 * #anynumber# = 0。

除法相同:0 / #anynumber# = 0。

你可以组织你的代码不使用这么多布尔标志,但这不是你的问题,无论如何。

于 2012-12-31T18:51:38.613 回答
1

正如 Dark Flacon 所说,您应该在按下等号按钮时进行计算。如果事实只是记住第一个数字,则不应调用total1总数:

Double lastNumber = 0;
Double total = 0;
bool minusButtonClicked = false;
bool plusButtonClicked = false;
bool divideButtonClicked = false;
bool multiplyButtonClicked = false;

private void btnPlus_Click(object sender, EventArgs e)
{
    lastNumber = Double.Parse(txtDisplay.Text);
    txtDisplay.Clear();

    plusButtonClicked = true;
    minusButtonClicked = false;
    divideButtonClicked = false;
    multiplyButtonClicked = false;
}

private void btnMinus_Click(object sender, EventArgs e)
{
    lastNumber = Double.Parse(txtDisplay.Text);
    txtDisplay.Clear();

    plusButtonClicked = false;
    minusButtonClicked = true;
    divideButtonClicked = false;
    multiplyButtonClicked = false;
}

private void btnDivide_Click(object sender, EventArgs e)
{
    lastNumber = Double.Parse(txtDisplay.Text);
    txtDisplay.Clear();

    plusButtonClicked = false;
    minusButtonClicked = false;
    divideButtonClicked = true;
    multiplyButtonClicked = false;
}

private void btnMultiply_Click(object sender, EventArgs e)
{
    lastNumber = Double.Parse(txtDisplay.Text);
    txtDisplay.Clear();

    plusButtonClicked = false;
    minusButtonClicked = false;
    divideButtonClicked = false;
    multiplyButtonClicked = true;
}

private void btnEquals_Click(object sender, EventArgs e)
{
    if (plusButtonClicked == true)
    {
        total = lastNumber + Double.Parse(txtDisplay.Text);
    }
    else if (minusButtonClicked == true)
    {
        total = lastNumber - Double.Parse(txtDisplay.Text);
    }
    else if (divideButtonClicked == true)
    {
        total = lastNumber / Double.Parse(txtDisplay.Text);
    }
    else if (multiplyButtonClicked == true)
    {
        total = lastNumber * Double.Parse(txtDisplay.Text);
    }

    txtDisplay.Text = total.ToString();
    lastNumber = 0;
}
于 2012-12-31T18:52:30.757 回答