mardi 31 mars 2015

c# quiz application running score

I am developing a quiz application that keeps a running score for example an incorrect answer gives -1 and a correct answer gives +1. It works fine but i am having trouble with the final question i would like my "next" button to change to a "results" button on the form and then on click display a message box displaying a message containing the score and a message. How could i implement this button change only when the answer has been submitted on the last question as i am catching an exception at the minute


here is my code:



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();
}


thanks for your help


Aucun commentaire:

Enregistrer un commentaire