2017-02-26 113 views
1

我有很多if和else语句,我想知道如何让它变得简短和甜美。此函数检查用户输入到文本框中的答案是否与(隐藏)数据网格中的答案相同。如果是同样的加1 correctAnswer - 其计算用户有多少正确答案有正确的(对错误的答案反之亦然)删除C#代码重复#

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

类型 'System.ArgumentOutOfRangeException' 的未处理的异常出现在mscorlib.dll

其他信息:索引超出范围。必须是非负数且小于集合的大小。

行:

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

你在第二个块中有错误:'if(firstAnswerCorrect == true)'。 –

+0

什么类型的bug?它似乎对我很好 – CsharpStudent

+0

看看第二**块的if条件。它使用'firstAnswerCorrect'变量而不是'secondAnswerCorrect'。 –

回答

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

对不起''标签[i] .Text =“不正确”;' - >'标签[i] .Text =“不正确”;但总的来说你可以给你的变量命名。只要确保你使用的是这个变量。如果你有任何问题,请让我知道是什么问题 –

+0

很好做 – CsharpStudent

+0

很高兴它帮助:) –

0

我所有的答案文本框和正确/不正确的答案标签存储在数组中,喜欢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; 
    } 
} 
+0

听起来不错,生病了试一试 – CsharpStudent

1

可以尽量减少通过以下方式重复:

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

简单的解决方案是改变前夕什么是字符串而不是布尔值。这样你就不需要任何if陈述。只要label1.Text = firstAnswerCorrect这等于(字符串)或者"correct""incorrect"

而对于功能,你将只返回"correct""incorrect"

希望这有助于。