私は多くの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]);
2番目のブロックにバグがあります: 'if(firstAnswerCorrect == true)'。 –
どのような種類のバグですか?それは私のためにうまくいくようです。 – CsharpStudent
**第2ブロックの 'if'条件を見てください。 'secondAnswerCorrect'の代わりに' firstAnswerCorrect'変数を使用しています。 –