2017-02-26 110 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"

希望這有助於。