Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#为什么intersect方法会返回这个?_C#_Arrays_Linq_Intersect_Except - Fatal编程技术网

c#为什么intersect方法会返回这个?

c#为什么intersect方法会返回这个?,c#,arrays,linq,intersect,except,C#,Arrays,Linq,Intersect,Except,我有两个数组,数组testAnswer保存“考试答案”,数组inputAnswers保存“学生考试答案” 我试图显示正确和错误的答案。换句话说,尝试显示testAnswer具有哪些inputAnswers不具有的值(错误答案),以及两个数组的共同值(正确答案) 为此,我使用了使用linq的.Except和.Intersect方法;然而,我得到了一个奇怪的输出: B, D, A, C 谁能帮帮我吗,我已经在这干了很久了 我的代码: private void button1_Click(objec

我有两个数组,数组testAnswer保存“考试答案”,数组inputAnswers保存“学生考试答案”

我试图显示正确和错误的答案。换句话说,尝试显示testAnswer具有哪些inputAnswers不具有的值(错误答案),以及两个数组的共同值(正确答案)

为此,我使用了使用linq的.Except和.Intersect方法;然而,我得到了一个奇怪的输出:

B, D, A, C
谁能帮帮我吗,我已经在这干了很久了

我的代码:

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

      var inter = inputAnswer.Intersect(testAnswer);
      foreach (var s in inter)
       {
          listBox1.Items.Add(s);
        }
     }   

Intersect
不设置交叉点,因此它会丢弃重复的值。如果要比较答案,更好的选择是并行遍历数组:

for(int i=0; i<testAnswer.Length; i++) {
    if(testAnswer[i] == inputAnswer[i])
        listBox1.Items.Add(inputAnswer[i]); // or testAnswer[i], as appropriate
}

for(int i=0;i您可以在
inputswer
中发布测试值吗?