2012-08-27 57 views
-1

我有兩個數組:數組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]); 
    } 
} 
+1

您是否嘗試過'listBox1.Items.Count'? –

+0

彼得格魯克已經給你答案了...只是想你想在哪裏顯示你的總數,並顯示「listBox1.Items.Count」和「listBox2.Items.Count」 – horgh

+0

除了你只顯示答案字母在你的列表框中,同時顯示問題也不會更好;說,'string.Format(「{0}({1})」,「問題#i」,inputAnswer [i])' – horgh

回答

0

下面的代碼將提供這將按住接聽結束2個整數:

private void button1_Click(object sender, EventArgs e) 
{ 
    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 
     else 
      listBox2.Items.Add(inputAnswer[i]); 
     } 

    int correctAns = listbox1.Items.Count; 
    int wringAns = listbox2.Items.Count; 
} 
-1

常見的答案計數將在Enumerable.Intersect結果項目數,罕見 - Enumerable.Except結果項目數。

更新:只要有人在評論中提到,它會產生錯誤的答案,證明它不會:

var testAnswers = new[] { 1, 2, 3 }; 
    var inputAnswers = new[] { 3, 2, 1 }; 
    var commonAnswers = testAnswers 
      .Select((x, index) => Tuple.Create(x, index)) 
      .Intersect(inputAnswers.Select((y, index) => Tuple.Create(y, index)));  
+2

這應該是一個評論。 – freebird

+2

這個答案還包括下降的積極因素,如下面的2個數組:{1,2,3} && {3,1,2}在相交時計算它的數字3.雖然答案是0. – SynerCoder

+0

@SynerCoder - 它取決於你將如何使用它,使用相交的代碼示例更新答案併產生正確的答案。 – Giedrius

4

下面介紹如何使用LINQ to得到結果:

var results = 
    testAnswer 
     .Zip(inputAnswer, (t, i) => new { t, i }) 
     .Aggregate(new { Correct = 0, Incorrect = 0 }, 
      (a, ti) => new 
      { 
       Correct = a.Correct + (ti.t == ti.i ? 1 : 0), 
       Incorrect = a.Incorrect + (ti.t != ti.i ? 1 : 0) 
      }); 

它會產生用這種結果的一個匿名變量:

anonymous variable result

另一種方法是:

var query = 
    testAnswer 
     .Zip(inputAnswer, (t, i) => t == i) 
     .ToLookup(x => x); 

var results = new 
{ 
    Correct = query[true].Count(), 
    Incorrect = query[false].Count() 
}; 
+1

不知道LINQ可以做到這一點,LINQ總是讓我驚歎不已,功能強大。 – SynerCoder

相關問題