c#为什么intersect方法会返回这个?
我有两个数组,数组testAnswer保存“考试答案”,数组inputAnswers保存“学生考试答案” 我试图显示正确和错误的答案。换句话说,尝试显示testAnswer具有哪些inputAnswers不具有的值(错误答案),以及两个数组的共同值(正确答案) 为此,我使用了使用linq的.Except和.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
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
中发布测试值吗?