我正在开发一个保持运行得分的测验应用程序。例如:错误答案给出-1,正确答案给出+1。它工作正常,但是 我在最后一个问题上遇到了麻烦。我希望我的“下一步”按钮更改为表单上的“结果”按钮,然后单击,显示一个消息框,显示包含分数和消息的消息。
只有在最后一个问题上提交了答案时,我才能实现这个按钮更改(因为我现在正在捕捉异常)?
这是我的代码:
int score = 0;
int i = -1;
int a = 0;
string[] questions = new string[]
{
"Question 1?",
"Question 2",
"Question 3",
"Question 4",
};
string[] answers = new string[]
{
"incorrect","correct","incorrect","incorrect",
"correct","incorrect","incorrect","incorrect",
"incorrect","incorrect","incorrect","correct",
"incorrect","incorrect","correct","incorrect"
};
string[] quizAnswers = new string[] { "correct", "correct", "correct", "correct" };
public FrmQuestion2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Tutorial3 t3 = new Tutorial3();
t3.Show();
}
private void FrmQuestion2_Load(object sender, EventArgs e)
{
}
private void Sbutton_Click(object sender, EventArgs e)
{
try
{
if (i < questions.Length)
i++;
txtQuestion.Text = questions[i];
RadA.Text = answers[a];
a++;
RadB.Text = answers[a];
a++;
RadC.Text = answers[a];
a++;
RadD.Text = answers[a];
a++;
Sbutton.Visible = true;
Sbutton.Enabled = true;
Sbutton.Text = "Submit";
// Subutton.Visible = true;
// Subutton.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("Only 4 questions available: Your Score is: " + score + " You need at least a score of 3 to progress");
}
if (score >= 3)
{
nxtbutton.Visible = true;
nxtbutton.Enabled = true;
}
else if (score < 3)
{
nxtbutton.Visible = false;
nxtbutton.Enabled = false;
RetryButton.Visible = true;
RetryButton.Enabled = true;
}
}
private void Subutton_Click(object sender, EventArgs e)
{
try
{
if (getSelected().Equals(quizAnswers[i]))
{
MessageBox.Show("Correct");
score++;
txtScore.Text = Convert.ToString("Score:" + score);
Subutton.Enabled = false;
Subutton.Visible = false;
Sbutton.Visible = true;
Sbutton.Enabled = true;
Sbutton.Text = "Next";
}
else
{
MessageBox.Show("Incorrect");
score--;
txtScore.Text = Convert.ToString("Score:" + score);
Subutton.Enabled = false;
Subutton.Visible = false;
Sbutton.Visible = true;
Sbutton.Enabled = true;
Sbutton.Text = "Next";
}
}
catch (Exception ex)
{
MessageBox.Show("Please start the quiz");
}
}
string getSelected()
{
if (RadA.Checked)
return RadA.Text.ToString();
if (RadB.Checked)
return RadB.Text.ToString();
if (RadC.Checked)
return RadC.Text.ToString();
if (RadD.Checked)
return RadD.Text.ToString();
return "";
}
private void RetryButton_Click(object sender, EventArgs e)
{
this.Hide();
FrmQuestion2 q2 = new FrmQuestion2();
q2.Show();
}
答案 0 :(得分:0)
这个答案的一些假设......
我猜你的nextButton的点击事件是Sbutton_Click(),而且还有一个submitButton,其click事件是Subutton_Click()。
我不认为您的Sbutton_Click()事件不应该进行任何分数检查,如果有要显示的话,它应该只显示下一个问题。您的Subutton_Click()可以检查所选答案,然后决定显示nextButton或resultsButton,因为您的程序应该知道最后一个问题是否已被回答。
然后,当单击结果按钮时,您可以检查您的分数并显示它,并确定是否显示/启用了重试按钮