【问题标题】:printing 2d array in c# through for loop通过for循环在c#中打印二维数组
【发布时间】:2012-11-26 00:09:53
【问题描述】:

这是我的归档函数,它从文件中获取数据并将其放入数组中:

public void populate_grid_by_file()
    {
        String store_data_from_file = string.Empty;
        try
        {
            using (StreamReader reader = new StreamReader("data.txt"))
            {
                store_data_from_file = reader.ReadToEnd().ToString();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        string[] line = store_data_from_file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < line.Length; i++)
        {
            string[] alphabet = store_data_from_file.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            for (int j = 0; j < alphabet.Length; j++)
            {
                Sodoku_Gri[i, j] = alphabet[j];
            }
        }            
    }

这是文件中写的内容:

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-

这是我想要打印的内容:

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 - 

我想过这样做:

public void display_grid()
    {
        for (int row = 0; row < Sodoku_Gri.GetLength(0); row++)
        {
            for (int col = 0; col < Sodoku_Gri.GetLength(1); col++)
            {

                Console.Write(Sodoku_Gri[row, col]);
                Console.Write("  ");
            }
            Console.WriteLine();
        }
    }

我只是不明白为什么二维数组打印了 9 次,最后一行有额外的 ------------!

它应该是一个二维数组,每个元素之间都有间距,就像我在文件中显示的数据一样,但是没有间距。

【问题讨论】:

  • 我不明白这个问题:isshould be 之间的唯一区别似乎是换行符,即显然是由您的电话 Console.WriteLine(); 创建的。哪里有“额外的--------”?
  • @O.R.Mapper 他不是在逃避 Markdown,现在看看。
  • 我看到的唯一区别是额外的空格。是这样吗?
  • 输出是二维数组打印了9次!

标签: c# visual-studio-2010 multidimensional-array


【解决方案1】:

您正在将数据分成几行,看起来您在内部循环中的意图是处理当前行中的字符。但是,您实际上是在为每一行迭代处理整个文件,这就是为什么您的输出包含文件的所有字符 * 行数的原因。试试这个调整:

   public void populate_grid_by_file()
        {
            String store_data_from_file = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader("data.txt"))
                {
                    store_data_from_file = reader.ReadToEnd().ToString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            string[] line = store_data_from_file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < line.Length; i++)
            {
                //string[] alphabet = store_data_from_file.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                string[] alphabet = line[i].Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries)
                for (int j = 0; j < alphabet.Length; j++)
                {
                    Sodoku_Gri[i, j] = alphabet[j];
                }
            }            
        }

【讨论】:

  • 先生会有什么变化??
  • 等一下,我刚刚发现你的变化
  • 不工作的网格;打印不正确!首先它只是重复打印的问题!现在连文件都没有正确加载!
  • 请先生再次阅读问题!已经进行了一些编辑!
  • @haris 好的,我已经更新了答案以包含 ENTIRE 函数,因为显然不理解只是更改相应的行。只需将整个函数复制/粘贴到 VS 中,然后重试。
猜你喜欢
  • 2015-06-12
  • 2013-08-02
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2018-07-19
  • 2014-02-02
  • 1970-01-01
相关资源
最近更新 更多