2 つの質問があります。
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(送信ボタン) が txtReminder(テキストボックス) を非表示にしないようにするにはどうすればよいですか? 現在「自動非表示」になっているのは、デフォルト値を「txtReminder.Visible = false;」に設定したためです。これにより、数学の問題が除算でない限り非表示になります。btnDiv_Click(分割ボタン) をクリックすると、「txtReminder.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
}
この質問が意味をなさない場合は、説明を求めてください。