2017-02-26 9 views
1

私は多くのif文とelse文を持っています。私はそれをどのように短くて甘くすることができるのだろうかと思います。この関数は、ユーザーがテキストボックスに入力した回答が、(隠された)データグリッドの回答と同じであるかどうかをチェックします。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"); 

コードはそのちょうど繰り返し

EDITワークスがありますどのように多くの正解計算 - それは同じである場合correctAnswerに1を追加この関数は正解と誤答を計算するだけです。私が言ったよう

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

2番目のブロックにバグがあります: 'if(firstAnswerCorrect == true)'。 –

+0

どのような種類のバグですか?それは私のためにうまくいくようです。 – CsharpStudent

+0

**第2ブロックの 'if'条件を見てください。 'secondAnswerCorrect'の代わりに' firstAnswerCorrect'変数を使用しています。 –

答えて

5

あなたは以下のように1 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

申し訳ありません、 'label [i] .Text =" wrong ";' - > 'labels [i] .Text =" incorrect ";'変数の名前を任意に指定します。その変数を使用していることを確認してください。もう一度問題がある場合は、問題が何であるかを教えてください –

+0

きれいに完了しました – CsharpStudent

+0

喜んでそれを助けました:) –

0

私はLabel[] answerLabelsTextBox[] answerTextBoxesのように、アレイ内のすべての回答のテキストボックスと正解/不正解のラベルを保存したいです。これらの配列の順序は正しく指定する必要があります。つまり、answerLabels[0]answerTextBoxes[0]は1番目の質問に対応し、answerLabels[1]answerTextBoxes[1]は2番目の質問に対応する必要があります。

その後、このループのための単一のようになります。

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"

希望を返します。