我是编程菜鸟,我似乎无法让这个计算器正常工作。我已经编写了所有按钮并允许键盘输入(我没有将其放入代码摘录中,因为它无关紧要)来自用户,但是就像标题所示,等于按钮不能用于多个总和。我不确定导致问题的是什么。任何建议将不胜感激。
这是我的代码。
public partial class MainWindow : Window
{
string input = string.Empty; //String storing user input
string operand1 = string.Empty; //String storing first operand
string operand2 = string.Empty; //String storing second operand
char operation; //char for operarion
double result = 0.0; //calculated result
bool operationCompleted = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Zero_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "0";
this.textBox.Text += input;
if(operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_One_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "1";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Two_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "2";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Three_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "3";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Four_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "4";
this.textBox.Text += input;
if (operationCompleted)
{
input = string.Empty;
textBox.Text = string.Empty;
operand1 = string.Empty;
operand2 = string.Empty;
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Five_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "5";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Six_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "6";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Seven_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "7";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Eight_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "8";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Nine_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "9";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Dot_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += ".";
this.textBox.Text += input;
btn_Equals.Focus();
}
private void btn_Minus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '-';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Plus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '+';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Multiply_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '*';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Divide_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '/';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
// The equals works for the first sum but not for any after it
private void btn_Equals_Click(object sender, RoutedEventArgs e)
{
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
if (operation == '+')
{
result = num1 + num2;
textBox.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
textBox.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox.Text = result.ToString();
}
else if (operation == '/')
{
if (num1 != 0 || num2 != 0)
{
result = num1 / num2;
textBox.Text = result.ToString();
}
else
{
textBox.Text = "Cannot divide by zero";
}
}
operationCompleted = true;
this.Focus();
}
}
private void btn_Clear_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
this.result = 0.0;
operationCompleted = false;
}
}
答案 0 :(得分:2)
您需要清除操作。你有操作+ =' /'。因此,每当有人单击其中一个操作按钮时,您都会将该操作附加到字符串。当您单击等于按钮时,您没有捕获错误操作,因此没有任何反应。
建议:
完成操作后清除操作。 添加一个默认的else if if来解决错误的操作,以便你可以处理错误。