【问题标题】:Show 2d Array row and column counter显示二维数组行列计数器
【发布时间】:2020-01-03 19:02:29
【问题描述】:

我想显示数组行和列计数器。希望你能理解我的问题。 图片在底部。

        char[,] arr = new char[4, 4]
        {
            { '☺', '♣', '♦', '♠'},
            { '♥', '♫', '☼', '☺'},
            { '☺', '♣', '♦', '♠'},
            { '♥', '♫', '☼', '☺'},
        };
        int rowLength = arr.GetLength(0);
        int colLength = arr.GetLength(1);
        for (int i = 0; i < rowLength; i++)
        {
            for (int j = 0; j < colLength; j++)
            {
                Console.Write(string.Format("\t{0} ", arr[i, j]));
            }
            Console.Write(Environment.NewLine + Environment.NewLine);
        }
        Console.ReadLine();

输出:Image without counter

代替:Image with counter

【问题讨论】:

  • 那么您要做的是以表格格式显示数组的内容,列号位于顶部,行号位于侧面?
  • 请不要将代码发布为图片

标签: c# multidimensional-array


【解决方案1】:

我猜你需要查看行号和列号。 您需要在代码中添加它们才能看到它们。 像这样的:

char[,] arr = new char[4, 4]
    {
        { '☺', '♣', '♦', '♠'},
        { '♥', '♫', '☼', '☺'},
        { '☺', '♣', '♦', '♠'},
        { '♥', '♫', '☼', '☺'},
    };
        int rowLength = arr.GetLength(0);
        int colLength = arr.GetLength(1);
        Console.Write("\t");
        for (int row =1; row<5; row++)
        {
            Console.Write(string.Format("\t{0}", row));
        }
        Console.Write(Environment.NewLine);
        for (int i = 0; i < rowLength; i++)
        {
            Console.Write(string.Format("\t{0} ", i+1));
            for (int j = 0; j < colLength; j++)
            {
                Console.Write(string.Format("\t{0} ", arr[i, j]));
            }
            Console.Write(Environment.NewLine + Environment.NewLine);
        }
        Console.ReadLine();

【讨论】:

  • 美丽的@Rumenand,它有效。感谢您节省了我的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多