我正在尝试读取文件,然后将值放入矩阵中。我收到输入字符串格式不正确的错误。它还有故障排除提示,“将字符串转换为 DateTime 时,在将每个变量放入 DateTime 对象之前解析字符串以获取日期。这很奇怪,因为我什至没有在任何地方使用 DateTime。下面是我的代码。我我确定我错过了一些愚蠢的东西。如果我忘记了您需要查看的代码的任何部分,请告诉我。
代码(C#):
class Program
{
private static string line;
private static int[,] matrix;
private static int numOfCities;
private static int startCity;
private static int[] greedyPath;
private static int greedyRecursionCount;
private static int total;
private static int lowerBoundTotal;
private static string filename = "points.txt";
static void Main(string[] args)
{
readTxtFile();
getStartCity();
greedyStart();
totalTour();
lowerBound();
compareAll();
Console.ReadKey();
}
private static void readTxtFile()
{
numOfCities = 0;
TextReader tr = new StreamReader(filename);
numOfCities = int.Parse(tr.ReadLine().Trim());
line = "1";
int index = 0;
matrix = new int[numOfCities,numOfCities];
for (int i = 0; i < numOfCities; i++)
{
line = tr.ReadLine();
for (int j = 0; j < numOfCities; j++)
{
matrix[i, j] = int.Parse(line[index].ToString().Trim());
index = index + 2;
}//end inner for loop
index = 0;
}//end outer for loop
tr.Close();
}
}
文件内容:
1000 //Number of points that are in the file
16 11 //XY-Points that are separated by 3 spaces
10 45
29 47
任何帮助,将不胜感激。先感谢您。