我只是想要一些代码方面的帮助。
我将其设置如下:
public partial class frmAdditionTutor : Form
{
public int I = 0;
public frmAdditionTutor()
{
InitializeComponent();
Random rand = new Random();
int NumberOne = rand.Next(500) + 100;
int NumberTwo = rand.Next(500) + 100;
lblEquation.Text = NumberOne.ToString() + " + " + NumberTwo.ToString() + "= ?";
}
private void btnSolve_Click(object sender, EventArgs e)
{
Random rand = new Random();
int NumberOne = rand.Next(500) + 100;
int NumberTwo = rand.Next(500) + 100;
int TotalAmount = NumberOne + NumberTwo;
int UserInputs = Convert.ToInt32(txtInput.Text);
if ((NumberOne + NumberTwo) == UserInputs)
{
lblRightorWrong.Text = "Correct!";
I++;
txtAmountCorrect.Text = I.ToString();
}
else
{
lblRightorWrong.Text = "Incorrect!";
I++;
txtQuestionCount.Text = I.ToString();
}
}
}
启动该程序时,会给用户一个方程式来解决。
当他们输入问题时,代码将检查其是否正确。如果答案正确,我希望它在Label
中显示正确!,然后将+1
添加到我的AmountCorrect
TextBox
中。 />
问题在于,无论用户是否正确输入代码,代码都认为它是错误的并且不会添加到计数器上。
我希望我的代码随机显示不同的变量,以使该人获得正确的答案。
答案 0 :(得分:2)
在代码中,您在单击求解按钮的同时创建了新的NumberOne和NumberTwo。 如果要再次创建随机问题,请在计算后再执行一次。您还可以使用一种方法来使代码可重复使用。
public partial class frmAdditionTutor : Form
{
public int CorrectAnswerCount = 0;
public int IncorrectAnswerCount = 0;
int NumberOne = 0;
int NumberTwo = 0;
public Form1()
{
InitializeComponent();
CreateRandomQuestion();
}
private void btnSolve_Click(object sender, EventArgs e)
{
int TotalAmount = NumberOne + NumberTwo;
int UserInputs = Convert.ToInt32(txtInput.Text);
if (TotalAmount == UserInputs)
{
lblRightorWrong.Text = "Correct!";
CorrectAnswerCount++;
txtAmountCorrect.Text = CorrectAnswerCount.ToString();
}
else
{
lblRightorWrong.Text = "Incorrect!";
IncorrectAnswerCount++;
txtAmountWrong.Text = IncorrectAnswerCount.ToString();
}
CreateRandomQuestion();
}
public void CreateRandomQuestion()
{
Random rand = new Random();
NumberOne = rand.Next(500) + 100;
NumberTwo = rand.Next(500) + 100;
lblEquation.Text = NumberOne.ToString() + " + " + NumberTwo.ToString() + "= ?";
txtInput.Clear();
}
}