计算器分而倍增不给出预期的结果?

时间:2012-12-31 18:45:46

标签: c# calculator

我的计算器代码出现问题。加号和减号按预期工作,但是除数和乘法每次最终得到ZERO的答案。这是代码:

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语句中是否存在某些内容或其他常见错误?

2 个答案:

答案 0 :(得分:3)

您将total1和total2初始化为0.

您的代码:

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

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

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

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

答案 1 :(得分: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;
}