gpt4 book ai didi

C# For 循环不递增

转载 作者:行者123 更新时间:2023-11-30 20:50:12 27 4
gpt4 key购买 nike

我这里有一些代码,应该打开一个文本文件并解析它。

由Tabs、换行符解析

据我所知,它应该将解析后的数据存储在二维数组中。

数组[行,数据]

所以

           System.IO.FileInfo enemyFile = new System.IO.FileInfo("Data\\enemies.txt"); 
System.IO.StreamReader enemies = enemyFile.OpenText();
string line;
string[,] enemyInfo = new string[20,20]; // Array to store parsed text

while ((line = enemies.ReadLine()) != null)
{
string[] items = line.Split('\n');
string[] newItems;
for (int i = 0; i < items.Length; i++)
{
Console.WriteLine(i);
newItems = items[i].Split('\t');
for (int i2 = 0; i2 < newItems.Length; i2++)
{
enemyInfo[i, i2] = newItems[i2];
//testArray.ListArray(newItems);
Console.WriteLine("[{0},{1}] = {2}", i, i2, enemyInfo[i, i2]);
Console.ReadLine();

}
Console.WriteLine("-");

}

应该将第一行的第一个解析数据放入enemyInfo[0,0],将第一行的下一个解析数据放入enemyInfo[0,1],依此类推。

在断行处,它应该开始将数据存储在 enemyInfo[1,0] 中,然后是 enemyInfo[1,1] 等等。

  1. 敌人.txt

    Name of Race    Race_ID Class_ID    Sex_ID  ToHit   Evade   Damage  Strength    Dexterity   Constitution    Intelligence    Charisma    Wisdom  Experience  Level
    Goblin 0 0 2 0 1 -1 6 8 6 4 4 4 1 1
    Kobold 1 0 2 1 1 0 8 8 8 6 4 4 3 2

难道只有我做错了什么?无论我尝试什么,它都不会在第一个 for 循环中递增 i,因此它会一直将新行存储在数组的同一维度中。

希望我已经提供了足够的信息。

提前致谢。

//罗尼亨里克森

编辑:

忘记添加我得到的输出示例。

[0,0] = Name of race
[0,1] = Race
and so on up to [0,14] and then it does this:
[0,0] = Goblin
[0,1] = 0
and so on, up to [0,14] and then it does the same with the next line ( kobold ).

最佳答案

你的错误是用\n分割ReadLine读取的行,你应该直接用\t分割这一行

   int i = 0;
while ((line = enemies.ReadLine()) != null)
{
string[] items = line.Split('\t');
for (int i2 = 0; i2 < items.Length; i2++)
{
Console.WriteLine(i2);
enemyInfo[i, i2] = items[i2];
Console.WriteLine("[{0},{1}] = {2}", i, i2, enemyInfo[i, i2]);
Console.ReadLine();
}
i++;
}

关于C# For 循环不递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22743138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com