Roebotek Roebotek - 17 days ago 4
C# Question

Create a Multiple Choice Quiz with arrays in C#

I have to create a quiz using a string array and a running score will be displayed, there will be one point added for each correct answer and once point subtracted for each wrong answer. I've got the first question working fine I just don't know how to do it for the next ones. I only have one submit button so all the code for the first question is connected to that button. How do I make it so when you submit your second answer it tells you its correct then moves on?
I have been told a for loop would work well with this but I don't know how to implement it.

int score = 0;
int i = -1;
int a = 0;

string[] questions = new string[] {
"What is 9 cubed?",
"What is 6+3?",
"What type of animal is tuna sandwiches made from?",
"What is 18 backwards?" };

string[] answers = new string[] {
"9", "81", "729", "2", "4", "2",
"9", "1", "zebra", "aardvark",
"fish", "gnu", "31",
"81", "91", "88" };

private void btnStart_Click(object sender, EventArgs e)
{
if (i < questions.Length)
i++;
//txtScore.Text = score;

lblQuestion.Text = questions[i];

radA.Text = answers[a];
a++;
radB.Text = answers[a];
a++;
radC.Text = answers[a];
a++;
radD.Text = answers[a];
a++;

btnStart.Visible = false;
btnStart.Enabled = false;
btnSubmit.Visible = true;
btnSubmit.Enabled = true;

}

private void btnSubmit_Click(object sender, EventArgs e)
{
{
if (i == 0 && radB.Checked)
{
MessageBox.Show("Correct");
score++;
txtScore.Text = Convert.ToString(score);
btnSubmit.Enabled = false;
btnSubmit.Visible = false;
btnStart.Visible = true;
btnStart.Enabled = true;
btnStart.Text = "Next";
}

else
{
MessageBox.Show("Incorrect");
score--;
}

Answer

problems : here you have hardcoded answer with radio button b value as below:

if (i == 0 && radB.Checked)

it will only check answer with radio button b and it will only work for first question.

you are not continuing this process for rest of the Questions.

solution: i have added one strng array which contain all quiz answers for your questions. so when user press submit button it will verify with respective answer and continue the same process till the end.

code as below:

int score = 0;
int i = -1;
int a = 0;

string[] questions = new string[]
{
    "What is 9 cubed?", "What is 6+3?", 
    "What type of animal is tuna sandwiches made from?",
    "What is 18 backwards?"
};

string[] answers = new string[] {
   "9", "81", "729", "2", 
   "4", "2", "9", "1", 
   "zebra", "aardvark", "fish", "gnu", 
   "31", "81", "91", "88"
};

string [] quizAnswers=new string[]{"729","9","aardvark","81"};
private     void btnStart_Click(object sender, EventArgs e)
{
    if (i < questions.Length)
    i++;
    //txtScore.Text = score;

    lblQuestion.Text = questions[i];

    radA.Text = answers[a];
    a++;
    radB.Text = answers[a];
    a++;
    radC.Text = answers[a];
    a++;
    radD.Text = answers[a];
    a++;

    btnStart.Visible = false;
    btnStart.Enabled = false;
    btnSubmit.Visible = true;
    btnSubmit.Enabled = true;

}

private void btnSubmit_Click(object sender, EventArgs e){


    if(getSelectedAnswer().Equals(quizAnswers[i]))
    {
        MessageBox.Show("Correct");
        score++;
        txtScore.Text = Convert.ToString(score);
        btnSubmit.Enabled = false;
        btnSubmit.Visible = false;
        btnStart.Visible = true;
        btnStart.Enabled = true;
        btnStart.Text = "Next";
    }

    else
    {
        MessageBox.Show("Incorrect");
        score--;
        txtScore.Text = Convert.ToString(score);
        btnSubmit.Enabled = false;
        btnSubmit.Visible = false;
        btnStart.Visible = true;
        btnStart.Enabled = true;
        btnStart.Text = "Next";
    }
}
string getSelectedAnswer()
{
    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 "";
}
Comments