在 Equal_Click 中获得结果后,也许你应该执行
'input = result.ToString()'。 跨度>
I'm new to C# and I'm having difficulties with this calculator code. Everything works find until you try to do something with the result of the calculation. For example if i do 8*2 and get 16, i cant add/subtract/multiply/divide from this result. Here is
the code.
namespace Calculator
{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
}
private void Zero_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
else if (textBox1.Text == "0")
{
}
}
private void One_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void Two_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void Three_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void Four_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void Five_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void Six_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void Seven_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void Eight_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void Nine_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void Decimal_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += ".";
this.textBox1.Text += input;
}
private void Divide_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '/';
input = string.Empty;
}
private void Multiply_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '*';
input = string.Empty;
}
private void Plus_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '+';
input = string.Empty;
}
private void Minus_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '-';
input = string.Empty;
}
private void Clear_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
}
private void Equal_Click(object sender, EventArgs e)
{
operand2 = input;
double.TryParse(operand1, out double num1);
double.TryParse(operand2, out double num2);
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
if (operation == '/')
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox1.Text = result.ToString();
}
else if (operation == '+')
{
result = num1 + num2;
textBox1.Text = result.ToString();
}
else if (operation == '-')
{
if (num2 != 0)
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "ERROR: cannot divide by 0";
}
}
}
}
}
Conner Ferguson
Maybe you should execute
‘input = result.ToString()’ after obtaining the result in Equal_Click.