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

3 回答 3

2

好的。在这里很简单,您的价值观在回发时令人耳目一新。所以只需将值保存在视图状态中。在此之前,请减少您的代码行。

你有

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;

    }

类似地减少 add、sub、mul、divide 运算符单击事件的代码,就像我刚刚在上面为数字按钮单击事件显示的那样。并在 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();

希望这可以帮助

于 2013-05-29T06:20:24.170 回答
1

在我看来,您的问题出在您的 ASP.Net 环境和/或您的 IDE 中,而不是在您的代码中。我不太了解 ASP.Net,但我确实了解 C#,并且我注意到以下两个奇怪的事实:

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

  • 但是,您的代码永远不会设置firstOperandnull. 它确实将它设置为空字符串( "") in clearVariables,但这与null( "" != null) 不同。

因此,我必须得出结论,您的代码以外的其他内容设置firstOperand为空。最合乎逻辑的来源是您的执行/调试环境,它在初始化执行或调用新页面、类、方法等时将所有对象和字符串变量重置为 null(对于范围为这些的任何变量) )。

当然,您不希望它在每次单击按钮时都这样做,所以我必须假设您的环境/设置中有一些错误导致了这种情况。

希望比我更了解 ASP.Net 的人可以解释其余的...

于 2013-05-28T13:34:26.350 回答
0

Yes At last I found it.

It is the problem with sessions. Every time I click button, a new session invokes and all the value resets. So We need to add values in session and recover it.

Like :

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

and at page load:

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)
    {
    }

It is not a good solution I think(still studying asp :) ). And I can use if condition since number of item is fixed to 3.

于 2013-05-29T04:31:19.160 回答