重複の可能性:
for ループを介して c# で 2 次元配列を出力する
これはtxtファイルの私のデータです:
1--2--3-- 3-4-4-5-- -7-3-4--- 7--5--3-6 --7---4-- 3-2--4-5- ------3-- 2-6--7--- 4---4--3-
空行なしで!上記のフォーマットの問題があります!
ディスプレイでもファイル読み取りを行うための私のC#コードです:
public void populate_grid_by_file()
{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("data.txt");
for (int i = 0; i < Sodoku_Gri.GetLength(0); i++)
{
while ((line = file.ReadLine()) != null)
{
for (int j = 0; j < Sodoku_Gri.GetLength(1); j++)
{
Sodoku_Gri[i,j] = line[j];
Console.Write(line[j].ToString());
}
Console.WriteLine(line);
counter++;
}
}
file.Close();
// Suspend the screen.
Console.ReadLine();
}
しかし、上記のファイルを読み取って配列を表示すると、次のようになります。
1--2--3--1--2--3-- 3-4-4-5--3-4-4-5-- -7-3-4----7-3-4--- 7--5--3-67--5--3-6 --7---4----7---4-- 3-2--4-5-3-2--4-5- ------3--------3-- 2-6--7---2-6--7--- 4---4--3-4---4--3-
なぜ重複するのかわかりません!ヘルプ!
デバッグすると、次の行に問題があることがわかりました。
Console.Write(line[j].ToString());
ここでは、要素が自動的に配列にロードされていないことを意味します。
Sodoku_Gri[i,j] = line[j];
これで私を助けてください!