我有两个数组,数组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);
}
}
答案 0 :(得分:3)
Intersect
确实设置了交集,因此它会丢弃重复值。如果你想比较答案,更好的选择是并行完成数组:
for(int i=0; i<testAnswer.Length; i++) {
if(testAnswer[i] == inputAnswer[i])
listBox1.Items.Add(inputAnswer[i]); // or testAnswer[i], as appropriate
}