无法识别的数组错误

时间:2012-10-14 09:56:02

标签: c# arrays

以下是产生一些错误,即“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");

三江源

5 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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 }; 

答案 2 :(得分:1)

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

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

string correctAnswers = "BDAACABACDBCDADCCBDA";

然后像:

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

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

答案 3 :(得分: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");

答案 4 :(得分:0)

你有一个string的数组。如您所知(或不知道),字符串是双引号 - " &#34 ;.所以在您的问题中,string []应该如下所示:

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