我想寫一些Unity代碼的C#代碼,它將從文本文件讀取,將每行存儲在字符串數組中,然後將其轉換爲2D字符數組。發生C#錯誤:不能將字符串[]'轉換爲'字符串'
錯誤的:
void ReadFile()
{
StreamReader read = new StreamReader(Application.dataPath + "/Maze1.txt");
int length = read.ReadLine().Length;
maze = new string[length, length];
line = new string[length];
while(!read.EndOfStream)
{
for (int i = 0; i <= length; i++)
{
line[i] = read.ReadLine();
}
for(int i = 0; i <= length; i++)
{
for(int j = 0; j <= length; j++)
{
maze[i,j] = line[i].Split(','); // <---This line is the issue.
}
}
}
}
我得到確切的錯誤是:
Cannot Implicitly convert type 'string[]' to 'string'
這個錯誤是什麼意思,我如何修復代碼?
這一行:INT長度= read.ReadLine ()。長度;將推進一行的流,然後在for循環中,你從第2行開始讀取。 – thalm
是的,這很好,文件的長度,寬度和高度都是相同的。 – user2709291
但是你需要文本的第一行還是不行?因爲在你的代碼中,第一行會被忽略。 – thalm