删除C#中的代码重复

Csh*_*ent 1 c# code-duplication

我有很多if和else语句,我想知道如何让它简短而甜蜜.此函数检查用户在文本框中输入的答案是否与(隐藏)数据网格中的答案相同.如果它是相同的添加1到correctAnswer - 它计算用户有多少正确的答案(反之亦然,错误答案)

bool firstAnswerCorrect = CheckAnswer(dataGridView1.Rows[0], textBoxQ1);
        if (firstAnswerCorrect == true)
        {
            label1.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label1.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool secondAnswerCorrect = CheckAnswer(dataGridView1.Rows[1], textBoxQ2);
        if (firstAnswerCorrect == true)
        {
            label2.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label2.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool thirdAnswerCorrect = CheckAnswer(dataGridView1.Rows[2], textBoxQ3);
        if (thirdAnswerCorrect == true)
        {
            label3.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label3.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool fourthAnswerCorrect = CheckAnswer(dataGridView1.Rows[3], textBoxQ4);
        if (fourthAnswerCorrect == true)
        {
            label4.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label4.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool fifthAnswerCorrect = CheckAnswer(dataGridView1.Rows[4], textBoxQ5);
        if (fifthAnswerCorrect == true)
        {
            label5.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label5.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool sixthAnswerCorrect = CheckAnswer(dataGridView1.Rows[5], textBoxQ6);
        if (sixthAnswerCorrect == true)
        {
            label6.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label6.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool seventhAnswerCorrect = CheckAnswer(dataGridView1.Rows[6], textBoxQ7);
        if (seventhAnswerCorrect == true)
        {
            label7.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label7.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool eighthAnswerCorrect = CheckAnswer(dataGridView1.Rows[7], textBoxQ8);
        if (eighthAnswerCorrect == true)
        {
            label8.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label8.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool ninethAnswerCorrect = CheckAnswer(dataGridView1.Rows[8], textBoxQ9);
        if (ninethAnswerCorrect == true)
        {
            label9.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label9.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        bool tenthAnswerCorrect = CheckAnswer(dataGridView1.Rows[9], textBoxQ10);
        if (tenthAnswerCorrect == true)
        {
            label10.Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            label10.Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
        label11.Text = ("YOU HAVE SCORED " + correctAnswers + " OUT OF 10");
        label12.Text = ("YOU HAVE " + wrongAnswers + " QUESTIONS WRONG");
Run Code Online (Sandbox Code Playgroud)

代码工作只是重复

编辑:

此功能只计算正确和错误的答案.我有另一个函数,它检查用户输入到文本框中的答案是否与(隐藏)数据网格中的答案相同

这是该函数的代码:

 private bool CheckAnswer(DataGridViewRow dataGridViewRow, TextBox textBox)
    {
        string correctAnswer = dataGridViewRow.Cells["answer"].Value.ToString();
        string givenAnswer = textBox.Text;

        bool isCorrect = string.Equals(correctAnswer, givenAnswer, StringComparison.CurrentCultureIgnoreCase);

        return isCorrect;
    }
Run Code Online (Sandbox Code Playgroud)

就像我说的.它的工作,但它只是重复,这不是一个好兆头.

编辑:

这是我使用的新C#代码,它消除了代码重复,但是当我稍微调整它时遇到异常.

 List<TextBox> textboxes = new List<TextBox> { textBoxQ1, textBoxQ2, textBoxQ3, textBoxQ4, textBoxQ5, textBoxQ6, textBoxQ7, textBoxQ8, textBoxQ9, textBoxQ10 };
        List<Label> labels = new List<Label> { label1, label2, label3, label4, label5, label6, label7, label8, label9, label10 };
        bool temp;
        for (int i = 0; i < 10; i++)
        {
            temp = CheckAnswer(dataGridView1.Rows[i], textboxes[i]);
            if (temp == true)
            {
                labels[i].Text = "correct";
                correctAnswers = correctAnswers + 1;

            }
            else
            {
                labels[i].Text = "incorrect";
                wrongAnswers = wrongAnswers + 1;
            }
            label11.Text = ("YOU HAVE SCORED " + correctAnswers);
            label12.Text = ("YOU HAVE SCORED " + correctAnswers);
        }
Run Code Online (Sandbox Code Playgroud)

mscorlib.dll中发生未处理的"System.ArgumentOutOfRangeException"类型异常

附加信息:指数超出范围.必须是非负数且小于集合的大小.

这条线:

 temp = CheckAnswer(dataGridView1.Rows[i], textboxes[i]);
Run Code Online (Sandbox Code Playgroud)

Raj*_*gaj 5

你可以像下面这样做一个List<TextBox>又一个List<Label>:

List<TextBox> textboxes = new List<TextBox>{textbox1, ....}
List<Label> labels = new List<Label>{label1, label2, ....}
bool temp;
for(int i = 0; i < 10; i++)
{
    temp = CheckAnswer(dataGridView1.Rows[i], textBoxes[i]);
    if (temp)
    {
        labels[i].Text = "correct";
        correctAnswers = correctAnswers + 1;
    }
    else
    {
        labels[i].Text = "incorrect";
        wrongAnswers = wrongAnswers + 1;
    }
}
Run Code Online (Sandbox Code Playgroud)