我有两个问题:
1)这是使变量的默认值= 0,然后将用户给定的值传递给该变量的正确方法吗?
protected void btnCheck_Click(object sender, EventArgs e)
{
lblYesNo.Text = "";
//default int values are set to 0
int remainder = 0;
int guess = 0;
remainder = int.Parse(txtRemainder.Text);
guess = int.Parse(txtAnswer.Text);
answer = (int)Session["answer"];
if (guess == answer)
{
lblYesNo.Text = lblYesNo.Text + "Correct!";
}
else
{
lblYesNo.Text = lblYesNo.Text + "Try Again..";
}
}//END Check Answer
2)如何阻止 Check_Click(提交按钮)隐藏 txtRemainder(文本框)?它现在“自动隐藏”的原因是因为我将默认值设置为“txtRemainder.Visible = false;” 在 Page_Load 中,除非数学问题是除法,否则它将使其隐藏。当我单击 btnDiv_Click(divide button) 时,它会将其重置为“txtRemainder.Visible = true;”,因为这提供了一个待解决的除法问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class BasicMath : System.Web.UI.Page
{
int number1;
int number2;
int answer;
protected void Page_Load(object sender, EventArgs e)
{
txtRemainder.Visible = false;
}
protected void GetRandom()
{
Random rand = new Random();
number1 = rand.Next(0, 10);
number2 = rand.Next(0, 10);
txtAnswer.Text = "";
txtRemainder.Text = "";
lblYesNo.Text = "";
}//END Get Random Number
protected void btnAdd_Click(object sender, EventArgs e)
{
GetRandom();
lblEquation.Text = number1.ToString() + " + " + number2.ToString();
answer = number1 + number2;
Session["answer"] = answer;
}//END Addition Button
protected void btnSub_Click(object sender, EventArgs e)
{
GetRandom();
if (number2 > number1)
{
answer = number2 - number1;
lblEquation.Text = number2.ToString() + " - " + number1.ToString();
}
else
{
answer = number1 - number2;
lblEquation.Text = number1.ToString() + " - " + number2.ToString();
}
Session["answer"] = answer;
}//END Subtraction Button
protected void btnMult_Click(object sender, EventArgs e)
{
GetRandom();
lblEquation.Text = number1.ToString() + " x " + number2.ToString();
answer = number1 * number2;
Session["answer"] = answer;
}//END Multiplication Button
protected void btnDiv_Click(object sender, EventArgs e)
{
Random rand = new Random();
number1 = rand.Next(1, 10);
number2 = rand.Next(1, 10);
/*will only display the txtRemainder(textbox) while using the Divide button,
txtRemainder will auto-hide when using another button because
it's default setting of '.Visible = false' is placed in the Page_Load*/
txtRemainder.Visible = true;
lblEquation.Text = number1.ToString() + " / " + number2.ToString();
answer = number1 / number2;
Session["answer"] = answer;
}//END Division Button
protected void btnCheck_Click(object sender, EventArgs e)
{
lblYesNo.Text = "";
//default int values are set to 0
int remainder = 0;
int guess = 0;
remainder = int.Parse(txtRemainder.Text);
guess = int.Parse(txtAnswer.Text);
answer = (int)Session["answer"];
if (guess == answer)
{
lblYesNo.Text = lblYesNo.Text + "Correct!";
}
else
{
lblYesNo.Text = lblYesNo.Text + "Try Again..";
}
}//END Check Answer
}
如果这个问题没有意义,请要求澄清。