C#计算器应用程序 [英] C# calculator application
问题描述
这是我试图制作的计算器代码.我有它的工作,除了我有一个错误,我似乎无法弄清楚如何解决它.
This is the code for the calculator in which i have been attempting to make. I have got it to work except i have one error that i cant seem to figure out how to fix it.
每当用户单击错误的操作员然后单击他们希望使用的正确操作员时(+按钮-按钮ect)都不会更改为正确的操作员,并且执行的功能将不正确.
whenever the user clicks the wrong operator then clicks the correct one that they wish to use the operator (+ button - button ect)will not change to the correct one and the function carried out will be in incorrect one.
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()
中)时设置,因此似乎很清楚为什么第二次单击不会改变事情.
You're using math_operator
to store the selected math operator.
Its value is only set when second_click == false
(in btn_operator_Click()
), therefore it seems clear why the second click doesn't change things.
这篇关于C#计算器应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!