-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]);
    }
}
4

3 回答 3

4

以下是使用 LINQ 获取结果的方法:

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)
            });

它将产生一个具有这种结果的匿名变量:

匿名变量结果

另一种方法是:

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

var results = new
{
    Correct = query[true].Count(),
    Incorrect = query[false].Count()
};
于 2012-08-27T07:34:18.903 回答
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;
}
于 2012-08-27T07:30:09.863 回答
-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)));      
于 2012-08-27T07:21:46.527 回答