首页 > 解决方案 > 显示二维数组行列计数器

问题描述

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

        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();

输出:没有计数器的图像

代替:带计数器的图像

标签: c#multidimensional-array

解决方案


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

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();

推荐阅读