-1

在类定义中

 public String[,] Sodoku_Gri = new String [9, 9];

    public void populate_grid_by_file()
    {
        TextReader tr = new StreamReader("data.txt");

        // read a line of text
        String store_data_from_file =  tr.ReadLine();
        for (int i = 0; i < Sodoku_Gri.GetLength(0); i++)
        {
            for (int j = 0; j < Sodoku_Gri.GetLength(1); j++)
            {

                Sodoku_Gri[i, j] = __________??
            }
        }
        tr.Close();
    }

在data.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# 中的二维数组中!在 C++ 中很容易。我是初学者!在 C++ 中,我们也应该在字符串中进行索引以访问字符串中的每个字符!我可以在我的二维数组中写入这些数据吗?这样 Sodoku_Grid[9,9] 中的 81 个空格都被文件中的数据填充了!

4

2 回答 2

0

假设您的 Sodoku_Gri 是以这种方式声明的二维字符数组

char[,] Sodoku_Gri = new char[9,9];

并且该行包含数独游戏的已知数字的位置,则应以这种方式计算正确字符的索引

Sodoku_Gri[i, j] = store_data_from_file[i*9+j];

(顺便说一句,该行导致无效的数独模式)

编辑:然后在下面看到您的评论,如果 Sodoku_Gri 被声明为

string[,] Sodoku_Gri = new string[9,9];

那么您需要将字符串转换添加到索引字符

Sodoku_Gri[i, j] = store_data_from_file[i*9+j].ToString();
于 2012-12-07T18:05:38.443 回答
0
  1. 您可能想将您的移动tr.ReadLine()到最里面的循环中。
  2. 您可以使用索引器访问字符串中的单个字符:

    Sodoku_Gri[i,j] = store_data_from_file[j]

所以在 C# 中也很容易。

于 2012-12-07T17:55:40.060 回答