How to print 2D array to console in C#

后端 未结 10 1596
野性不改
野性不改 2020-12-06 22:59

I dont\'t have any code for this, but I do want to know how I could do this. I use visual studio 2010 C# if that matters.

Thanks

Jason

10条回答
  •  离开以前
    2020-12-06 23:32

    Try like this..

            int[,] matrix = new int[3, 3]
            {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9},
            };
    
            int rowLength = matrix.GetLength(0);
            int colLength = matrix.GetLength(1);
    
            for (int i = 0; i < rowLength; i++)
            {
                for (int j = 0; j < colLength; j++)
                {
                    Console.Write(string.Format("{0} ", matrix[i, j]));
                }
                Console.Write(Environment.NewLine + Environment.NewLine);
            }
    
    
            Console.Read();
    

提交回复
热议问题