C#计算器应用程序
C# calculator application
这是我一直在尝试制作的计算器的代码。我已经让它工作了,除了我有一个错误,我似乎无法弄清楚如何修复它。
每当用户点击错误的运算符然后点击他们希望使用的正确运算符(+ 按钮 - 按钮等)时,将不会更改为正确的运算符,并且所执行的功能将是错误的。
double value_1 = 0;
bool double_operation = false;
bool clickable_decimal = true;
bool operation_pressed = false;
bool second_click = false;
bool second_equals = false;
String math_operator = "";
private void btn_Click(object sender, EventArgs e)
{
if (second_equals == true)
{
txt_display.Text = "0";
//value_1 = 0;
second_equals = false;
}
if ((txt_display.Text == "0") || (operation_pressed == true))
{
txt_display.Clear();
}
operation_pressed = false;
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
double_operation = false;
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_display.Text = "0";
value_1 = 0;
}
private void btn_deci_Click(object sender, EventArgs e)
{
{
if (clickable_decimal == true)
{
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
clickable_decimal = false;
}
}
}
private void btn_operator_Click(object sender, EventArgs e)
{
if (double_operation == false)
{
if (second_click == false)
{
Button btn = (Button)sender;
math_operator = btn.Text;
value_1 = double.Parse(txt_display.Text);
operation_pressed = true;
clickable_decimal = true;
second_equals = false;
}
if (second_click == true)
{
Button btn = (Button)sender;
//value_2 = double.Parse(txt_display.Text);
operation_pressed = true;
clickable_decimal = true;
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
math_operator = btn.Text;
second_equals = true;
}
second_click = true;
}
double_operation = true;
}
private void btn_equals_Click(object sender, EventArgs e)
{
if (second_equals == false)
{
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
second_click = false;
second_equals = true;
}
您正在使用 math_operator
来存储选定的数学运算符。
它的值仅在 second_click == false
时设置(在 btn_operator_Click()
中),因此第二次点击没有改变的原因似乎很清楚。
我找到了答案,任何对此感兴趣的人都在这里。
double value_1 = 0;
bool double_operation = false;
bool clickable_decimal = true;
bool operation_pressed = false;
bool second_click = false;
bool second_equals = false;
String math_operator = "";
private void btn_Click(object sender, EventArgs e)
{
if (second_equals == true)
{
txt_display.Text = "0";
//value_1 = 0;
second_equals = false;
}
if ((txt_display.Text == "0") || (operation_pressed == true))
{
txt_display.Clear();
}
operation_pressed = false;
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
double_operation = false;
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_display.Text = "0";
value_1 = 0;
}
private void btn_deci_Click(object sender, EventArgs e)
{
{
if (clickable_decimal == true)
{
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
clickable_decimal = false;
}
}
}
private void btn_operator_Click(object sender, EventArgs e)
{
//==========================================================================
if (double_operation == true)
{
math_operator = "";
double_operation = false;
second_click = false; // however it will work without this but i
//do not know why so ill leave it in
}
// this is the solution
//=========================================================================
if (double_operation == false)
{
if (second_click == false)
{
Button btn = (Button)sender;
math_operator = btn.Text;
value_1 = double.Parse(txt_display.Text);
operation_pressed = true;
clickable_decimal = true;
second_equals = false;
}
if (second_click == true)
{
Button btn = (Button)sender;
operation_pressed = true;
clickable_decimal = true;
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
math_operator = btn.Text;
second_equals = true;
}
second_click = true;
}
double_operation = true;
}
private void btn_equals_Click(object sender, EventArgs e)
{
if (second_equals == false)
{
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
second_click = false;
second_equals = true;
}
通过点击错误的运算符,它会将 math_operator
重置为空,然后将 double_operation
设置为 false,以便在用户点击正确的运算符时可以执行下面的代码。
这是我一直在尝试制作的计算器的代码。我已经让它工作了,除了我有一个错误,我似乎无法弄清楚如何修复它。
每当用户点击错误的运算符然后点击他们希望使用的正确运算符(+ 按钮 - 按钮等)时,将不会更改为正确的运算符,并且所执行的功能将是错误的。
double value_1 = 0;
bool double_operation = false;
bool clickable_decimal = true;
bool operation_pressed = false;
bool second_click = false;
bool second_equals = false;
String math_operator = "";
private void btn_Click(object sender, EventArgs e)
{
if (second_equals == true)
{
txt_display.Text = "0";
//value_1 = 0;
second_equals = false;
}
if ((txt_display.Text == "0") || (operation_pressed == true))
{
txt_display.Clear();
}
operation_pressed = false;
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
double_operation = false;
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_display.Text = "0";
value_1 = 0;
}
private void btn_deci_Click(object sender, EventArgs e)
{
{
if (clickable_decimal == true)
{
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
clickable_decimal = false;
}
}
}
private void btn_operator_Click(object sender, EventArgs e)
{
if (double_operation == false)
{
if (second_click == false)
{
Button btn = (Button)sender;
math_operator = btn.Text;
value_1 = double.Parse(txt_display.Text);
operation_pressed = true;
clickable_decimal = true;
second_equals = false;
}
if (second_click == true)
{
Button btn = (Button)sender;
//value_2 = double.Parse(txt_display.Text);
operation_pressed = true;
clickable_decimal = true;
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
math_operator = btn.Text;
second_equals = true;
}
second_click = true;
}
double_operation = true;
}
private void btn_equals_Click(object sender, EventArgs e)
{
if (second_equals == false)
{
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
second_click = false;
second_equals = true;
}
您正在使用 math_operator
来存储选定的数学运算符。
它的值仅在 second_click == false
时设置(在 btn_operator_Click()
中),因此第二次点击没有改变的原因似乎很清楚。
我找到了答案,任何对此感兴趣的人都在这里。
double value_1 = 0;
bool double_operation = false;
bool clickable_decimal = true;
bool operation_pressed = false;
bool second_click = false;
bool second_equals = false;
String math_operator = "";
private void btn_Click(object sender, EventArgs e)
{
if (second_equals == true)
{
txt_display.Text = "0";
//value_1 = 0;
second_equals = false;
}
if ((txt_display.Text == "0") || (operation_pressed == true))
{
txt_display.Clear();
}
operation_pressed = false;
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
double_operation = false;
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_display.Text = "0";
value_1 = 0;
}
private void btn_deci_Click(object sender, EventArgs e)
{
{
if (clickable_decimal == true)
{
Button btn = (Button)sender;
txt_display.Text = txt_display.Text + btn.Text;
clickable_decimal = false;
}
}
}
private void btn_operator_Click(object sender, EventArgs e)
{
//==========================================================================
if (double_operation == true)
{
math_operator = "";
double_operation = false;
second_click = false; // however it will work without this but i
//do not know why so ill leave it in
}
// this is the solution
//=========================================================================
if (double_operation == false)
{
if (second_click == false)
{
Button btn = (Button)sender;
math_operator = btn.Text;
value_1 = double.Parse(txt_display.Text);
operation_pressed = true;
clickable_decimal = true;
second_equals = false;
}
if (second_click == true)
{
Button btn = (Button)sender;
operation_pressed = true;
clickable_decimal = true;
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
math_operator = btn.Text;
second_equals = true;
}
second_click = true;
}
double_operation = true;
}
private void btn_equals_Click(object sender, EventArgs e)
{
if (second_equals == false)
{
switch (math_operator)
{
case "+":
txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
break;
case "-":
txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
break;
case "/":
txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
break;
case "*":
txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
break;
}//end switch
value_1 = double.Parse(txt_display.Text);
second_click = false;
second_equals = true;
}
通过点击错误的运算符,它会将 math_operator
重置为空,然后将 double_operation
设置为 false,以便在用户点击正确的运算符时可以执行下面的代码。