【问题标题】:Removing code duplication in C#删除 C# 中的代码重复
【发布时间】:2017-02-26 13:09:09
【问题描述】:

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

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

代码的工作原理只是重复

编辑:

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

这是该函数的代码:

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

正如我所说。它有效,但只是重复,这不是一个好兆头。

编辑:

这是我正在使用的新 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);
        }

在 mscorlib.dll 中发生了“System.ArgumentOutOfRangeException”类型的未处理异常

附加信息:索引超出范围。必须为非负数且小于集合的大小。

行:

 temp = CheckAnswer(dataGridView1.Rows[i], textboxes[i]);

【问题讨论】:

  • 你在第二个区块有错误:if (firstAnswerCorrect == true).
  • 什么类型的错误?这对我来说似乎很好用
  • 查看 second 块的if 条件。它使用firstAnswerCorrect 变量而不是secondAnswerCorrect
  • 啊,是的,我现在看到了。谢谢
  • 如果在codereview.stackexchange.com 上询问这可能会更合适

标签: c# code-duplication


【解决方案1】:

您可以创建一个List&lt;TextBox&gt; 和一个List&lt;Label&gt;,如下所示:

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

【讨论】:

  • 抱歉label[i].Text = "incorrect"; 有错别字 --> labels[i].Text = "incorrect"; 但通常你可以随意命名你的变量。只要确保您使用的是该变量。如果您再次遇到任何问题,请告诉我是什么问题
  • 很高兴它有帮助:)
  • 哎呀!当我稍微调整它时反驳了一个错误。它工作得很好,但是当我添加“你已经从 *** 中得分 ***”位时,抛出了一个异常。检查我编辑的问题的底部。
  • 小心索引,确保列表中有 10 个文本框和标签。此外,请确保您在 dataGridView1 中有 10 行。它们需要具有相同的大小。在 for 循环中,您可以将最大索引从 10 更改为 textboxes.Length
  • 我犯的愚蠢错误。我尝试在不从数据库中检索问题并将其放入数据网格的情况下计算总数。再次感谢
【解决方案2】:

可以尝试通过以下方式减少重复:

List<TextBox> textBoxList = new List<TextBox>() {
    textBoxQ1,
    textBoxQ2,
    ...
    textBoxQN
};

List<Label> labelList = new List<Label>() {
    label1,
    label2,
    ...
    labelN
};

for(int i = 0; i < textBoxList.Count; i++) {
    bool answerCorrect = CheckAnswer(dataGridView1.Rows[i], textBoxList[i]);
    labelList[i].Text = answerCorrect ? "correct" : "incorrect";
    correctAnswers += answerCorrect ? 1 : 0;
    wrongAnswers += !answerCorrect ? 1 : 0;
}

【讨论】:

    【解决方案3】:

    我会将所有答案文本框和正确/错误答案标签存储在一个数组中,例如Label[] answerLabelsTextBox[] answerTextBoxes。这些数组应该正确排序,即answerLabels[0]answerTextBoxes[0]应该对应第一个问题,answerLabels[1]answerTextBoxes[1]应该对应第二个问题等等。

    此后这变成了一个单独的 for 循环:

    for(int i = 0; i < answerLabels.Length; i++) {
        bool answerCorrect = CheckAnswer(dataGridView1.Rows[i], answerTextBoxes[i]);
        if (answerCorrect == true)
        {
            answerLabels[i].Text = "correct";
            correctAnswers = correctAnswers + 1;
        }
        else
        {
            answerLabels[i].Text = "incorrect";
            wrongAnswers = wrongAnswers + 1;
        }
    }
    

    【讨论】:

      【解决方案4】:

      一个简单的解决方案是将所有内容更改为字符串而不是布尔值。这样您就不需要任何if 语句。只是label1.Text = firstAnswerCorrect 等于(字符串)"correct""incorrect"

      对于函数,您只需返回 "correct""incorrect"

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多