使用C#中的数组创建多项选择测验

时间:2013-11-11 03:26:04

标签: c# arrays

我必须使用字符串数组创建测验并显示运行分数,每个正确答案将添加一个点,并为每个错误答案减去一个点。我有第一个问题正常工作我只是不知道如何为下一个问题做这件事。我只有一个提交按钮,所以第一个问题的所有代码都连接到该按钮。当你提交第二个答案时,我怎么做才能告诉你它的正确然后继续? 我被告知for循环可以很好地使用它,但我不知道如何实现它。

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

1 个答案:

答案 0 :(得分:2)

问题: 在这里你有单选按钮b值的硬编码答案如下:

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

它只会用单选按钮b检查答案,它只适用于第一个问题。

对于其他问题,你不会继续这个过程。

<强>溶液: 我添加了一个strng数组,其中包含您问题的所有测验答案。 因此,当用户按下提交按钮时,它将验证相应的答案并继续相同的过程直到结束。

代码如下:

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 "";
}