所以我试图读取字符串 theMap 中的 txt 文件,然后在地图中复制 theMap 并返回地图。我还尝试读取二维字符串中的 Txt 文件。然后返回的数组是map。我想在控制台上打印它,并且存在“无法将 [] 索引应用于‘方法组’类型的表达式”问题,这里也是代码。
public static string[,] Reader()
{
string theMap = System.IO.File.ReadAllText(@"C:\Users\Public\Console Slayer\Map\map.txt");
int k = 0, l = 0;
string[,] map = new string[11,54];
foreach (var row in theMap.Split('\n'))
{
foreach (var col in row.Trim().Split(' '))
{
map[l,k] = (col.Trim());
l++;
}
k++;
}
return map;
}
public static void Printer()
{
for (int y = 0; y < 11; y++)
{
for (int x = 0; x < 54; x++)
{
Console.Write(Reader[y,x]);
}
Console.WriteLine();
}
}
static void Main()
{
Reader();
Printer();
}
Reader
是一种方法。您无法对其建立索引,但可以对result of it:
Console.Write(Reader()[y,x]);
// ^ You need these parens to invoke the method.
然而,这将为每个循环调用该函数,读取文件 11 * 54 = 594 次!读取文件一次并存储结果;无需在每次循环迭代时调用此方法:
var data = Reader();
for (int y = 0; y < 11; y++)
{
for (int x = 0; x < 54; x++)
{
Console.Write(data[y,x]);
}
Console.WriteLine();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)