2

以下是创建一些错误,即“B”无法识别(以及其他 20 个字母和“文件”无法识别。

我看不到这里的问题?

string[] correctAnswers = {B,D,A,A,C,A,B,A,C,D,B,C,D,A,D,C,C,B,D,A}; 
string[] studentResults = File.ReadAllLines("studentResults.txt");

谢谢

4

5 回答 5

2

您应该将它们用双引号括起来:

string[] correctAnswers = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"}; 

编辑 :

(感谢@goldenparrot)你需要添加这个命名空间:

using System.IO;

能够使用 File.ReadAllLines()。要么:

string[] studentResults = System.IO.File.ReadAllLines("studentResults.txt");
于 2012-10-14T09:57:33.340 回答
2
const string A = "A";
const string B = "B";
const string C = "C";
const string D = "D";

string[] correctAnswers = { B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A }; 
于 2012-10-14T09:58:42.467 回答
1

正如其他人所说,您必须用双引号将字符串文字括起来。

现在,如果你想维护一个字符数组,最好使用一个简单的字符串而不是字符串数组,因为字符串也是可迭代的:

string correctAnswers = "BDAACABACDBCDADCCBDA";

然后是这样的:

foreach (char answer in correctAnswers) {
    // ...
}

关于您问题的第二部分,File类位于System.IO命名空间中,因此您需要一个using System.IO;指令,或者必须通过其完全限定名称来引用该类,System.IO.File.

于 2012-10-14T10:00:30.390 回答
0

将字母写在双引号内,并@用于文件的路径。

string[] correctAnswers = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"};
 string[] studentResults = File.ReadAllLines(@"studentResults.txt");
于 2012-10-14T09:58:40.373 回答
0

你有一个数组string。如您所知(或不知道),字符串用双引号 - “”。因此,在您的问题中, string[] 应该如下所示:

string[] correctAnswers = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A"}; 
于 2012-10-14T09:59:10.703 回答