0

我正在尝试制作一个将生成 2 个数字的简单网站,然后用户需要回答将其中 2 个数字相加的正确结果。

这是我的 ASPX:

<div id="questionContainer">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>  
        <div id="firstNumber">
            <%=firstNum %>
        </div>
        <div id="operator">
            <%=operatorString %>
        </div>
        <div id="secondNumber">
           <%=secondNum %>
        </div>
        <div id="answerWrapper">

                    <input type="text" id="answer" name="answer" placeholder="your answer" />

            <asp:Button runat="server" ID="submit" name="submit" OnClick="AnswerQuestion" Text="Answer" />
              <input type="hidden" name="op" id="op" value="1" />
            <div id="error">
                <%=textForUser %>
            </div>
            </ContentTemplate>
            </asp:UpdatePanel>
        </div>

这是代码隐藏:

protected static int firstNum;
    protected  static int secondNum;
    protected bool operatorTrueOrFalse = true;
    protected string operatorString = "+";
    protected GameLogic gl;
    protected static int answer;
    protected bool isItTrue;
    protected int userAnswer;
    protected string textForUser ="hello";
    protected string op;

    protected void Page_Load(object sender, EventArgs e)
    {



        if (Page.IsPostBack || op == "1") // if solved X or null.
        {
            gl = new GameLogic();
            firstNum = gl.GenerateNumber(4);

            secondNum = gl.GenerateNumber(4);

            answer = gl.SolveFor(firstNum, secondNum, operatorString);
        }


    }

    public void AnswerQuestion(object sender, EventArgs e)
    {
        if (Request["answer"] != null)
        {
            userAnswer = int.Parse(Request["answer"]);

        }
        else
        {
            textForUser = "You need to type something";
        }


        if (answer == userAnswer)
        { // user has answered correctly.
            isItTrue = true;
            textForUser = "very good";
            op = "1";
        }
        else
        {// user has answered in - correctly.
            isItTrue = false;
            textForUser = "you are wrong";
            op = "0";
        }
    }

问题是我注意到每次我尝试在文本框中回答问题时,数字都会改变。就像我没有回答正确的问题一样。

4

2 回答 2

3

问题是您使用的是静态变量。这些在所有请求中共享。不要为此目的使用静态变量,而是将它们存储在其他地方(fe Session、ViewState、Hiddenfield、...)。

在 ASP.NET 应用程序中管理持久用户状态的九个选项

  • 应用
  • 饼干
  • 表单发布/隐藏表单域
  • 请求参数
  • 会议
  • ASP.NET 中的新状态容器
  • 缓存
  • 语境
  • 视图状态
  • Web.config 和 Machine.config 文件

...当然还有数据库。

于 2012-05-11T14:37:41.957 回答
3

您似乎在回发时重新生成了一个新的号码对。基本上,如果您打算重复停留在同一页面并简单地发布答案,那么逻辑应该是这样的:

1) Is this a postback?
   -> No: Generate a new value and store it in ViewState. Hold onto this value.
   -> Yes: Do we have a stored answer value in ViewState?
         -> No: Error? Something has gone wrong.
         -> Yes: Fetch that Value and hold on to it
2) On Button Click, Is the answer you entered the same as the generated value?
   -> No: Show Error
   -> Yes: Show Success, Then generate a new value and store it in ViewState

您的算法似乎仅在回发期间和每次回发时才生成新的答案对。

于 2012-05-11T14:38:02.067 回答