我在asp.net的学习阶段,所以决定做一个在线计算器。问题是当我进行计算1 + 5 =
时,它根本没有给出任何输出。我尝试了调试。
Click button 1 :
first value = 1;
click button + :
first value = null;
click button 5 :
first value = 5
click button =
NOTHING :)
这是我的 C# 代码:
public partial class _Default : System.Web.UI.Page
{
string firstOperand;
string secondOperand;
string Operator;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOff_Click(object sender, EventArgs e)
{
txtScreen.Enabled = false;
ClearVariables();
}
protected void btnOn_Click(object sender, EventArgs e)
{
txtScreen.Enabled = true;
ClearVariables();
}
private void ClearVariables()
{
firstOperand = "";
secondOperand = "";
Operator = "";
}
protected void Operand(string value)
{
if (value == null) return;
try
{
txtScreen.Text = value;
if (firstOperand == null)
{
firstOperand = value;
}
else
{
if (Operator == null)
{
firstOperand.Insert(firstOperand.Length, value);
}
else
{
secondOperand.Insert(secondOperand.Length, value);
}
}
}
catch (Exception ex)
{
}
}
protected void Num1_Click(object sender, EventArgs e)
{
txtScreen.Text = Num1.Text;
Operand(Num1.Text);
}
protected void Num2_Click(object sender, EventArgs e)
{
txtScreen.Text = Num2.Text;
Operand(Num2.Text);
}
protected void Num3_Click(object sender, EventArgs e)
{
txtScreen.Text = Num3.Text;
Operand(Num3.Text);
}
protected void Num4_Click(object sender, EventArgs e)
{
txtScreen.Text = Num4.Text;
Operand(Num4.Text);
}
protected void Num5_Click(object sender, EventArgs e)
{
txtScreen.Text = Num5.Text;
Operand(Num5.Text);
}
protected void Num6_Click(object sender, EventArgs e)
{
txtScreen.Text = Num6.Text;
Operand(Num6.Text);
}
protected void Num7_Click(object sender, EventArgs e)
{
txtScreen.Text = Num7.Text;
Operand(Num7.Text);
}
protected void Num8_Click(object sender, EventArgs e)
{
txtScreen.Text = Num8.Text;
Operand(Num8.Text);
}
protected void Num9_Click(object sender, EventArgs e)
{
txtScreen.Text = Num9.Text;
Operand(Num9.Text);
}
protected void Num0_Click(object sender, EventArgs e)
{
txtScreen.Text = Num0.Text;
Operand(Num0.Text);
}
protected void btnClr_Click(object sender, EventArgs e)
{
txtScreen.Text = "";
ClearVariables();
}
protected void OpDiv_Click(object sender, EventArgs e)
{
if (firstOperand != null)
{
txtScreen.Text = "";
Operator = OpDiv.Text;
}
}
protected void OpMul_Click(object sender, EventArgs e)
{
if (firstOperand != null)
{
txtScreen.Text = "";
Operator = OpMul.Text;
}
}
protected void OpSub_Click(object sender, EventArgs e)
{
if (firstOperand != null)
{
txtScreen.Text = "";
Operator = OpSub.Text;
}
}
protected void OpAdd_Click(object sender, EventArgs e)
{
if (firstOperand != null)
{
txtScreen.Text = "";
Operator = OpAdd.Text;
}
}
protected void OpEqual_Click(object sender, EventArgs e)
{
if (firstOperand == null && Operator == null)
{
return;
}
else if (firstOperand != null && Operator != null && secondOperand == null)
{
secondOperand = firstOperand;
}
else
{
double num1;
double num2;
try
{
num1 = Double.Parse(firstOperand);
num2 =Double.Parse(secondOperand);
{
switch (Operator)
{
case "+":
num1 += num2;
firstOperand = num1.ToString();
txtScreen.Text = firstOperand;
break;
case "-":
num1 -= num2;
firstOperand = num1.ToString();
txtScreen.Text = firstOperand;
break;
case "/":
if (num2 == 0)
{
txtScreen.Text = "Divison by zero";
}
else
{
num1 /= num2;
firstOperand = num1.ToString();
txtScreen.Text = firstOperand;
}
break;
case "*":
num1 *= num2;
firstOperand = num1.ToString();
txtScreen.Text = firstOperand;
break;
default: txtScreen.Text = "Invalid Operation";
break;
}
}
}
catch (Exception ex)
{
txtScreen.Text = "Not a valid Number";
ClearVariables();
}
}
ClearVariables();
}
protected void OpDot_Click(object sender, EventArgs e)
{
if (firstOperand != null)
{
if (Operator == null)
{
firstOperand.Insert(firstOperand.Length, ".");
}
else
{
secondOperand.Insert(secondOperand.Length, ".");
}
}
}
}
有人可以解释发生了什么吗?以及如何解决同样的问题。
谢谢