我有一个问题,我想解决,但我不知道如何。我做了一个只有几个按钮的简单计算器(按钮1,按钮5,按钮+和按钮等于)。我想让Button Equal重复上一次操作(在这种情况下是加法)。当我按5 + 1并得到结果6时,它工作正常,但在那之后,当我再次按下按钮时,我没有得到结果7但我得到12。 我根本不知道我的错误在哪里以及如何重复功能并且不添加其他按钮。
我希望有人尽快回复并帮助解决我的问题。 :)
string input = string.Empty;
string oper1 = string.Empty;
string oper2 = string.Empty;
char operacija;
double rezultat = 0.0;
bool rez = false;
bool repeating= false;
private void buttonNumber1_Click(object sender, RoutedEventArgs e)
{
this.textBoxPrikazi.Text = "";
if (rez)
{
input = "1";
rez = false;
}
else
{
input += "1";
}
this.textBoxPrikazi.Text += input;
}
private void buttonNumber5_Click(object sender, RoutedEventArgs e)
{
this.textBoxPrikazi.Text = "";
if (rez)
{
input = "5";
rez = false;
}
else
{
input += "5";
}
this.textBoxPrikazi.Text += input;
}
private void buttonPlus_Click(object sender, RoutedEventArgs e)
{
textBoxPrikazi.Text = "+";
oper1 = input;
operacija = '+';
input = string.Empty;
}
private void buttonEquals_Click(object sender, RoutedEventArgs e)
{
oper2 = input;
double num1, num2;
double.TryParse(oper1, out num1);
double.TryParse(oper2, out num2);
this.textBoxPrikazi.Text = "0";
this.input = string.Empty;
this.oper1 = string.Empty;
this.oper2 = string.Empty;
if (operacija == '+')
{
// x = 1 + 5
rezultat = num1 + num2;
textBoxPrikazi.Text = rezultat.ToString();
// for repeating
if (repeating== true)
{
// 6 = 6
rezultat = num2;
// 6+ 6 = 12
textBoxPrikazi.Text = (rezultat + num2).ToString();
repeating= false;
}
}
if (rezultat == 0)
{
textBoxPrikazi.Text = "0";
}
else
{
input = rezultat.ToString();
rez = true;
repeating= true; // for repeating
}
}
}