2013-05-28 132 views
1

我在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, "."); 
      } 
     } 
    } 
} 

有人能解释发生了什么事?以及如何解决相同的问题。

感谢

+4

你有没有通过您的代码加强在所有...?任何异常,错误?在你的'Operand'函数中,你可以捕获(Exception ex)',但不要对异常做任何事情。如果出现问题,我会在“catch”块中打印。你很难通过查看你的代码来判断问题是什么,使用调试器可能会帮助你更多。 – tnw

+0

我注意到诸如'txtScreen.Text = Num1.Text'这样的语句是多余的,看看Operand()是如何做到这一点的。我很好奇txtScreen.Text属性是否分配了正确的字符串(例如,“6”)?如果您在OpEqual_Click()中转到该分配,是firstOperand ==“6”?或者是Double.Parse的问题? – DonBoitnott

+0

没有更多异常和值被正确指定。 – Zigma

回答

2

好的。这里很简单,您的价值在回传时令人耳目一新。所以只需将值保存在viewstate中即可。 在此之前,请减少您的代码行。

你有

protected void Num5_Click(object sender, EventArgs e) 
{ 
    txtScreen.Text = Num5.Text; 
    Operand(Num5.Text); 
} 

角落找寻10这样的活动。所以首先要成为一个单一的事件像

protected void Num_Click(object sender, EventArgs e) 
{ 
    Button btn = (Button)sender; 
    txtScreen.Text = btn.Text; 
    Operand(btn.Text); 
} 

,并为每个数字键的操作数方法现在

分配此事件为Click事件做出类似

private void Operand(string value) 
    { 

    if(ViewState["FirstOperand"] == null) 
     ViewState["FirstOperand"] = value; 
    else if(ViewState["SecondOperand"] == null) 
     ViewState["SecondOperand"] = value; 

    } 

同样降低你的代码添加,分,多,分操作员点击事件,因为我刚刚显示数字按钮点击事件上面。 ,并在ViewState [“Operator”]中设置运算符值。

最后在您的OpEqual_Click事件中。像

if(ViewState["FirstOperand"] != null) 
firstOperand = ViewState["FirstOperand"].ToString(); 


if(ViewState["SecondOperand"] != null) 
secondOperand = ViewState["SecondOperand"].ToString(); 

if(ViewState["Operator"] != null) 
Operator = ViewState["Operator"].ToString(); 

希望initally设置第一和第二个操作数这有助于

+0

用于将代码简化为近似1/10的+1:P代码像魅力一样工作 – Zigma

1

在我看来,你的问题是在你的ASP.Net环境和/或你的IDE,而不是在你的代码。我不知道ASP.Net很好,但我知道C#和我注意到这两个奇怪的事实:

  • 你调试显示firstOperand被重置为null或每一个事件之后,当前操作数。

  • 但是,你的代码从未firstOperandnull。它确实将它设置为clearVariables中的空字符串(""),但与空("" != null)不同。

因此,我必须结束不是你的代码的其他的东西被设置firstOperand为null。最合乎逻辑的来源是你的执行/调试环境,当它初始化执行时,或者当它调用一个新的Page,Class,Method等等时,它会将所有的对象和字符串变量重置为null(对于任何变量)。

当然,您不希望它为每个按钮单击都这样做,所以我必须假定您的环境/设置中存在导致此问题的错误。

希望别人谁知道ASP.Net更好的话,我可以解释休息...

+0

+1,好的观察。 – Zigma

0

是最后我找到了它。

这是sessions的问题。每次点击按钮,新会话都会调用并重置所有值。所以我们需要在会话中添加值并恢复它。

像:

Session["Calc"] = firstOperand + ","; 
Session["Calc"] += secondOperand + ","; 
Session["Calc"] += Operator + ","; 

,并在页面加载:

try 
    { 
     var Data = Session["Calc"].ToString().Split(','); 
     if(Data[0] != "") 
      firstOperand = Data[0]; 
     if (Data[1] != "") 
     Operator = Data[1]; 
     if (Data[2] != "") 
     secondOperand = Data[2]; 
    } 
    catch(Exception ex) 
    { 
    } 

这不是一个很好的解决方案,我认为(还在学习ASP :))。我可以使用if条件,因为项目的数量固定为3.