0

我必须使用字符串数组创建一个测验,并且会显示一个运行分数,每个正确答案都会加一分,每个错误答案都会减一分。我的第一个问题工作正常,我只是不知道如何为下一个问题做。我只有一个提交按钮,所以第一个问题的所有代码都连接到该按钮。我该如何做到这一点,当您提交第二个答案时,它会告诉您正确然后继续?有人告诉我 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--;
        }
4

1 回答 1

2

问题: 在这里,您使用单选按钮 b 值硬编码答案,如下所示:

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

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

对于其余的问题,您不会继续此过程。

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

代码如下:

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 "";
}
于 2013-11-11T04:07:19.833 回答