我的计算器代码有问题。加号和减号按预期工作,但是除法和乘法每次都得到零的答案。这是代码:
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 语句中的内容还是其他常见错误?