我正在尝试制作一个将生成 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";
}
}
问题是我注意到每次我尝试在文本框中回答问题时,数字都会改变。就像我没有回答正确的问题一样。