C# 让我的计算器在按下“相等”按钮后获取新数字
所以我已经做了一段时间了,仍然不能让我的计算器做最后一件事 我知道了,谢谢 在我按下equal按钮或Tan、Sin、Cos或Mod按钮后,我希望我的计算器获得一个新的数字。基本上就像文本框中什么都没有一样,即使答案仍然存在。目前,所有号码都保留,新号码添加到末尾。我不希望这种情况发生 在计算之后,如果我按下一个数字按钮,我希望它清除屏幕并添加新的数字。下面是我到目前为止的代码C# 让我的计算器在按下“相等”按钮后获取新数字,c#,winforms,C#,Winforms,所以我已经做了一段时间了,仍然不能让我的计算器做最后一件事 我知道了,谢谢 在我按下equal按钮或Tan、Sin、Cos或Mod按钮后,我希望我的计算器获得一个新的数字。基本上就像文本框中什么都没有一样,即使答案仍然存在。目前,所有号码都保留,新号码添加到末尾。我不希望这种情况发生 在计算之后,如果我按下一个数字按钮,我希望它清除屏幕并添加新的数字。下面是我到目前为止的代码 public partial class Form1 : Form { public Form1() {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
private void AddButton_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "+";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void EqualButton_Click(object sender, EventArgs e)
{
Double SecondNumber;
SecondNumber = Convert.ToDouble(DisplayTextBox.Text);
switch (mathOperator)
{
case "+":
DisplayTextBox.Text = (FirstNumber + SecondNumber).ToString();
break;
case "-":
DisplayTextBox.Text = (FirstNumber - SecondNumber).ToString();
break;
case "*":
DisplayTextBox.Text = (FirstNumber * SecondNumber).ToString();
break;
case "/":
DisplayTextBox.Text = (FirstNumber / SecondNumber).ToString();
break;
default:
break;
}
}
private void button12_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
FirstNumber *= -1;
DisplayTextBox.Text = FirstNumber.ToString();
}
private void ButtonMinus_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "-";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void ButtonMultiply_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "*";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void ButtonDivide_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
mathOperator = "/";
DisplayTextBox.Text = "";
DisplayTextBox.Focus();
}
private void ButtonMod_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();
}
private void Button1_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void Button2_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "2";
}
private void Button3_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "3";
}
private void Button4_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "4";
}
private void Button5_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "5";
}
private void Button6_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "6";
}
private void Button7_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "7";
}
private void Button8_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "8";
}
private void Button9_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = DisplayTextBox.Text + "9";
}
private void ButtonClear_Click(object sender, EventArgs e)
{
DisplayTextBox.Text = "";
mathOperator = "";
}
private void ButtonOff_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void ButtonTan_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Tan(FirstNumber).ToString();
}
private void ButtonSin_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Sin(FirstNumber).ToString();
}
private void ButtonCos_Click(object sender, EventArgs e)
{
FirstNumber = Convert.ToDouble(DisplayTextBox.Text);
DisplayTextBox.Text = Math.Cos(FirstNumber).ToString();
}
private void Button0_Click(object sender, EventArgs e)
{
if (DisplayTextBox.Text.Length >= 1)
{
DisplayTextBox.Text = DisplayTextBox.Text + "0";
}
else
{
return;
}
}
private void DecimalButton_Click(object sender, EventArgs e)
{
if (DisplayTextBox.Text.Contains("."))
{
return;
}
else
{
DisplayTextBox.Text = DisplayTextBox.Text + ".";
}
}
}
将状态变量添加到类中。如果为true,并且选择了一个数字,则在写入数字之前清除显示并将其设置为false
单击“=”时,将其设置为true。将状态变量添加到类中。如果为true,并且选择了一个数字,则在写入数字之前清除显示并将其设置为false
单击“=”时,将其设置为true。将状态变量添加到类中。如果为true,并且选择了一个数字,则在写入数字之前清除显示并将其设置为false
单击“=”时,将其设置为true。将状态变量添加到类中。如果为true,并且选择了一个数字,则在写入数字之前清除显示并将其设置为false
单击“=”时,将其设置为真。根据简要回顾,似乎需要引入一个变量来跟踪计算器的状态,然后在按下“相等”按钮时,将状态设置为计算值。然后,在数字按钮(0-9)中,需要检查状态变量,并附加或替换显示文本 根据简要回顾,似乎需要引入一个变量来跟踪计算器的状态,然后当按下“相等”按钮时,将状态设置为计算值。然后,在数字按钮(0-9)中,需要检查状态变量,并附加或替换显示文本 根据简要回顾,似乎需要引入一个变量来跟踪计算器的状态,然后当按下“相等”按钮时,将状态设置为计算值。然后,在数字按钮(0-9)中,需要检查状态变量,并附加或替换显示文本 根据简要回顾,似乎需要引入一个变量来跟踪计算器的状态,然后当按下“相等”按钮时,将状态设置为计算值。然后,在数字按钮(0-9)中,需要检查状态变量,并附加或替换显示文本 创建此全局变量
private bool equationComplete = false;
将此添加到“等于”按钮的末尾
equationComplete = true;
将此添加到每个数字按钮的开头单击
if (equationComplete) DisplayTextBox.Text = "";
将此添加到每个数字按钮的末尾,然后单击事件处理程序
equationComplete = false;
创建此全局变量
private bool equationComplete = false;
将此添加到“等于”按钮的末尾
equationComplete = true;
将此添加到每个数字按钮的开头单击
if (equationComplete) DisplayTextBox.Text = "";
将此添加到每个数字按钮的末尾,然后单击事件处理程序
equationComplete = false;
创建此全局变量
private bool equationComplete = false;
将此添加到“等于”按钮的末尾
equationComplete = true;
将此添加到每个数字按钮的开头单击
if (equationComplete) DisplayTextBox.Text = "";
将此添加到每个数字按钮的末尾,然后单击事件处理程序
equationComplete = false;
创建此全局变量
private bool equationComplete = false;
将此添加到“等于”按钮的末尾
equationComplete = true;
将此添加到每个数字按钮的开头单击
if (equationComplete) DisplayTextBox.Text = "";
将此添加到每个数字按钮的末尾,然后单击事件处理程序
equationComplete = false;
向类中添加一个布尔变量,该变量表示操作是否刚刚完成的标志,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最初,该值将为false
,因为首次创建计算器时没有发生任何事情
现在,在equal button事件处理程序的末尾,将标志设置为true,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最后,在数字按钮的事件处理程序中,检查operationJustCompleted
标志是否为true,如果为true,则清除文本并将operationJustCompleted标志重置为false
;像这样:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
向类中添加一个布尔变量,该变量表示操作是否刚刚完成的标志,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最初,该值将为false
,因为首次创建计算器时没有发生任何事情
现在,在equal button事件处理程序的末尾,将标志设置为true,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最后,在数字按钮的事件处理程序中,检查operationJustCompleted
标志是否为true,如果为true,则清除文本并将operationJustCompleted标志重置为false
;像这样:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
向类中添加一个布尔变量,该变量表示操作是否刚刚完成的标志,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最初,该值将为false
,因为首次创建计算器时没有发生任何事情
现在,在equal button事件处理程序的末尾,将标志设置为true,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最后,在数字按钮的事件处理程序中,检查operationJustCompleted
标志是否为true,如果为true,则清除文本并将operationJustCompleted标志重置为false
;像这样:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
向类中添加一个布尔变量,该变量表示操作是否刚刚完成的标志,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最初,该值将为false
,因为首次创建计算器时没有发生任何事情
现在,在equal button事件处理程序的末尾,将标志设置为true,如下所示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
最后,在数字按钮的事件处理程序中,检查operationJustCompleted
标志是否为true,如果为true,则清除文本并将operationJustCompleted标志重置为false
;像这样:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Double FirstNumber;
string mathOperator = "";
bool operationJustCompleted = false;
private void EqualButton_Click(object sender, EventArgs e)
{
// Logic for calculation
operationJustCompleted = true;
}
private void Button1_Click(object sender, EventArgs e)
{
if(operationJustCompleted)
{
DisplayTextBox.Text = String.Empty;
operationJustCompleted = false;
}
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
private void EqualButton_Click(object sender, EventArgs e)
{
ClearDisplayBeforeNextTextEntry = true;
}
private void Button1_Click(object sender, EventArgs e)
{
// New code
ClearText();
// Old code
DisplayTextBox.Text = DisplayTextBox.Text + "1";
}
// Same for all other number buttons as above
private void ClearText()
{
if (ClearDisplayBeforeNextTextEntry)
{
DisplayTextBox.Text = "";
ClearDisplayBeforeNextTextEntry = false;
}
}
正如大卫·阿诺所说,s