我有兩個數組:數組testAnswer包含「考試答案」,數組inputAnswers包含「學生對考試的答案」。顯示兩個數組之間共同和不常見元素的總數?
當我運行我的代碼時,它顯示了兩個數組(正確答案)和非常規元素(不正確答案)的所有常見元素。但是,我不想實際顯示正確/錯誤的答案,而是希望能夠顯示正確/錯誤答案的總數。
我迄今爲止代碼:
private void button1_Click(object sender, EventArgs e)
{
//Array holding answers to test
string[] testAnswer = new string[20] { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
string a = String.Join(", ", testAnswer);
//Reads text file line by line. Stores in array, each line of the file is an element in the array
string[] inputAnswer = System.IO.File.ReadAllLines(@"C:\Users\Momo\Desktop\UNI\Software tech\test.txt");
string b = String.Join(", ", inputAnswer);
//Increments through array elements in both arrays and checks for matching elements. Displays in listBox.
for (int i = 0; i < testAnswer.Length; i++)
{
if (testAnswer[i] == inputAnswer[i])
listBox1.Items.Add(inputAnswer[i]); // or testAnswer[i], as appropriate
}
//Increments through array elements in both arrays and checks for uncommon elements. Displays in listBox.
for (int i = 0; i < testAnswer.Length; i++)
{
if (testAnswer[i] != inputAnswer[i])
listBox2.Items.Add(inputAnswer[i]);
}
}
您是否嘗試過'listBox1.Items.Count'? –
彼得格魯克已經給你答案了...只是想你想在哪裏顯示你的總數,並顯示「listBox1.Items.Count」和「listBox2.Items.Count」 – horgh
除了你只顯示答案字母在你的列表框中,同時顯示問題也不會更好;說,'string.Format(「{0}({1})」,「問題#i」,inputAnswer [i])' – horgh