如何使用嵌套for循环打印2d数组?

时间:2015-12-05 16:23:53

标签: c# arrays loops 2d

我如何将这个打印的阵列集中在控制台中间?

        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("0 1 2 3 4 5 6 7");
        Console.ResetColor();
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                Console.Write(BoardDisplay[j, i] + " ");
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(i);
            Console.ResetColor();
        }

1 个答案:

答案 0 :(得分:0)

我在这个片段中添加了几种技术来格式化控制台输出:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication9
{
    class Program
    {
        static int[,] BoardDisplay = new int[8, 8] { 
            { 1,2,3,4,5,6,7,8 }
            ,{ 1,2,3,4,5,6,7,8 }
            ,{ 1,2,3,4,5,6,7,8 }
            ,{ 1,2,3,4,5,6,7,8 }
            ,{ 1,2,3,4,5,6,7,8 }
            ,{ 1,2,3,4,5,6,7,8 }
            ,{ 1,2,3,4,5,6,7,8 }
            ,{ 1,2,3,4,5,6,7,8 }
        };

        static void Main(string[] args)
        {
            int space_for_each_cell = 5;
            var width = Console.WindowWidth;
            var height = Console.WindowHeight;

            int left = (width - (8* space_for_each_cell)) / 2;

            Console.ForegroundColor = ConsoleColor.Red;
            Console.CursorLeft = left;
            for (int i = 0; i < 8; i++)
            {
                Console.Write("{0,"+space_for_each_cell+"}", i);
            }
            Console.WriteLine();
            Console.ResetColor();
            for (int i = 0; i < 8; i++)
            {
                Console.CursorLeft = left;
                for (int j = 0; j < 8; j++)
                {
                    Console.Write("{0,"+space_for_each_cell+"}", BoardDisplay[j, i]);
                }
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0,"+space_for_each_cell+"}",i);
                Console.ResetColor();
            }
            Console.ReadKey();
        }

    }
}

应用此概念以满足您的需求。