我正在尝试将计算器编写为网站,
我在课堂上设置了 2 个变量来存储数字,
但每次我点击“+”或“-”按钮时,
变量回到初始状态。
这是我的代码:
public partial class _Default : System.Web.UI.Page
{
int choice = 0;
Boolean caluOrNot = false;
double before, after;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Btn1_Click(object sender, EventArgs e)
{
if (this.caluOrNot == false)
{
if (txtResult.Text.ToString() == "0")
{
txtResult.Text = "1";
}
else
{
txtResult.Text += "1";
}
}
else
{
txtResult.Text = "1";
}
}
protected void Btn2_Click(object sender, EventArgs e)
protected void Btn3_Click(object sender, EventArgs e)
protected void Btn4_Click(object sender, EventArgs e)
protected void Btn5_Click(object sender, EventArgs e)
protected void Btn6_Click(object sender, EventArgs e)
protected void Btn7_Click(object sender, EventArgs e)
protected void Btn8_Click(object sender, EventArgs e)
protected void Btn9_Click(object sender, EventArgs e)
protected void Btn0_Click(object sender, EventArgs e)
protected void BtnPoint_Click(object sender, EventArgs e)
{
txtResult.Text += ".";
}
protected void BtnJia_Click(object sender, EventArgs e)
{
this.choice = 1;
this.caluOrNot = true;
before = Double.Parse(txtResult.Text.ToString());
txtCalu.Text = before.ToString() + "+";
}
protected void BtnJen_Click(object sender, EventArgs e)
protected void BtnCheng_Click(object sender, EventArgs e)
protected void BtnChu_Click(object sender, EventArgs e)
protected void btnClear_Click(object sender, EventArgs e)
{
txtResult.Text = "0";
this.choice = 0;
}
protected void btnGo_Click(object sender, EventArgs e)
{
double a;
switch (this.choice)
{
case 1:
a = before + after;
txtResult.Text = a.ToString();
break;
case 2:
a = before - after;
txtResult.Text = a.ToString();
break;
case 3:
a = before * after;
txtResult.Text = a.ToString();
break;
case 4:
a = before / after;
txtResult.Text = a.ToString();
break;
default:
break;
}
}
}
作为代码,每次点击btnJia
都会触发 onclick 方法BtnJia_Click
选择将设置为“1”并设置为 true,但值会在完成caculOrNot
时变回 0 和 false 。BtnJia_Click
我该如何解决?