使用数字和字母在控制台应用程序中创建16x26网格

时间:2018-11-05 15:12:23

标签: c# console console-application

我正试图在consolle应用程序中创建一个16x26的网格,就像我在标题中说的那样,但是我什至不能将数字和字母放在正确的位置。然后,我必须使用Unicode字符,使用列和线来创建类似于地图的网格。就像一个矩阵,在字母和数字内都包含uinicode字符。有人可以帮我吗,我是新来的。这就是我要做的

static void Main(string[] args)
    {
        char y;  
        int x, letra;

        for (int j = 0; j < 16; j++)
        {
            for (int i = 0; i < 26; i++)
            {
                if (j==0 && i==0 || i == 1)
                {
                    Console.Write(" ");
                }
                else if(j==0 && i > 1)
                {
                    letra = 65 + i;
                    y = Convert.ToChar(letra);
                    Console.Write(y + " ");
                }
                else if(i==0 && j > 1)
                {
                    x = 1 + j;
                    Console.WriteLine(" ", x);
                }
                else if(j % 2 == 0 && j != 0 && i == 0)
                {
                    Console.WriteLine(x + "");
                }

            }
        }

1 个答案:

答案 0 :(得分:2)

这个小演示应用程序可以帮助您到达希望的位置:

static void Main(string[] args)
{
    var numberOfRows = 16;
    var numberOfCols = 26;
    for (var currentRow = 0; currentRow < numberOfRows; currentRow++)
    {
        for (var currentCol = 0; currentCol < numberOfCols; currentCol++)
        {
            Console.Write(" ({0:00}, {1:00})", currentRow, currentCol); //for each column in the current row, output some data (in this case, the coordinates of this cell formatted " (row, col)"
        }
        Console.WriteLine(); //once we've completed a row, write a new line to move down to the next row
    }
}

示例输出

 (00, 00) (00, 01) (00, 02) (00, 03) (00, 04) (00, 05) (00, 06) (00, 07) (00, 08) (00, 09) (00, 10) (00, 11) (00, 12) (00, 13) (00, 14) (00, 15) (00, 16) (00, 17) (00, 18) (00, 19) (00, 20) (00, 21) (00, 22) (00, 23) (00, 24) (00, 25)
 (01, 00) (01, 01) (01, 02) (01, 03) (01, 04) (01, 05) (01, 06) (01, 07) (01, 08) (01, 09) (01, 10) (01, 11) (01, 12) (01, 13) (01, 14) (01, 15) (01, 16) (01, 17) (01, 18) (01, 19) (01, 20) (01, 21) (01, 22) (01, 23) (01, 24) (01, 25)
 (02, 00) (02, 01) (02, 02) (02, 03) (02, 04) (02, 05) (02, 06) (02, 07) (02, 08) (02, 09) (02, 10) (02, 11) (02, 12) (02, 13) (02, 14) (02, 15) (02, 16) (02, 17) (02, 18) (02, 19) (02, 20) (02, 21) (02, 22) (02, 23) (02, 24) (02, 25)
 (03, 00) (03, 01) (03, 02) (03, 03) (03, 04) (03, 05) (03, 06) (03, 07) (03, 08) (03, 09) (03, 10) (03, 11) (03, 12) (03, 13) (03, 14) (03, 15) (03, 16) (03, 17) (03, 18) (03, 19) (03, 20) (03, 21) (03, 22) (03, 23) (03, 24) (03, 25)
 (04, 00) (04, 01) (04, 02) (04, 03) (04, 04) (04, 05) (04, 06) (04, 07) (04, 08) (04, 09) (04, 10) (04, 11) (04, 12) (04, 13) (04, 14) (04, 15) (04, 16) (04, 17) (04, 18) (04, 19) (04, 20) (04, 21) (04, 22) (04, 23) (04, 24) (04, 25)
 (05, 00) (05, 01) (05, 02) (05, 03) (05, 04) (05, 05) (05, 06) (05, 07) (05, 08) (05, 09) (05, 10) (05, 11) (05, 12) (05, 13) (05, 14) (05, 15) (05, 16) (05, 17) (05, 18) (05, 19) (05, 20) (05, 21) (05, 22) (05, 23) (05, 24) (05, 25)
 (06, 00) (06, 01) (06, 02) (06, 03) (06, 04) (06, 05) (06, 06) (06, 07) (06, 08) (06, 09) (06, 10) (06, 11) (06, 12) (06, 13) (06, 14) (06, 15) (06, 16) (06, 17) (06, 18) (06, 19) (06, 20) (06, 21) (06, 22) (06, 23) (06, 24) (06, 25)
 (07, 00) (07, 01) (07, 02) (07, 03) (07, 04) (07, 05) (07, 06) (07, 07) (07, 08) (07, 09) (07, 10) (07, 11) (07, 12) (07, 13) (07, 14) (07, 15) (07, 16) (07, 17) (07, 18) (07, 19) (07, 20) (07, 21) (07, 22) (07, 23) (07, 24) (07, 25)
 (08, 00) (08, 01) (08, 02) (08, 03) (08, 04) (08, 05) (08, 06) (08, 07) (08, 08) (08, 09) (08, 10) (08, 11) (08, 12) (08, 13) (08, 14) (08, 15) (08, 16) (08, 17) (08, 18) (08, 19) (08, 20) (08, 21) (08, 22) (08, 23) (08, 24) (08, 25)
 (09, 00) (09, 01) (09, 02) (09, 03) (09, 04) (09, 05) (09, 06) (09, 07) (09, 08) (09, 09) (09, 10) (09, 11) (09, 12) (09, 13) (09, 14) (09, 15) (09, 16) (09, 17) (09, 18) (09, 19) (09, 20) (09, 21) (09, 22) (09, 23) (09, 24) (09, 25)
 (10, 00) (10, 01) (10, 02) (10, 03) (10, 04) (10, 05) (10, 06) (10, 07) (10, 08) (10, 09) (10, 10) (10, 11) (10, 12) (10, 13) (10, 14) (10, 15) (10, 16) (10, 17) (10, 18) (10, 19) (10, 20) (10, 21) (10, 22) (10, 23) (10, 24) (10, 25)
 (11, 00) (11, 01) (11, 02) (11, 03) (11, 04) (11, 05) (11, 06) (11, 07) (11, 08) (11, 09) (11, 10) (11, 11) (11, 12) (11, 13) (11, 14) (11, 15) (11, 16) (11, 17) (11, 18) (11, 19) (11, 20) (11, 21) (11, 22) (11, 23) (11, 24) (11, 25)
 (12, 00) (12, 01) (12, 02) (12, 03) (12, 04) (12, 05) (12, 06) (12, 07) (12, 08) (12, 09) (12, 10) (12, 11) (12, 12) (12, 13) (12, 14) (12, 15) (12, 16) (12, 17) (12, 18) (12, 19) (12, 20) (12, 21) (12, 22) (12, 23) (12, 24) (12, 25)
 (13, 00) (13, 01) (13, 02) (13, 03) (13, 04) (13, 05) (13, 06) (13, 07) (13, 08) (13, 09) (13, 10) (13, 11) (13, 12) (13, 13) (13, 14) (13, 15) (13, 16) (13, 17) (13, 18) (13, 19) (13, 20) (13, 21) (13, 22) (13, 23) (13, 24) (13, 25)
 (14, 00) (14, 01) (14, 02) (14, 03) (14, 04) (14, 05) (14, 06) (14, 07) (14, 08) (14, 09) (14, 10) (14, 11) (14, 12) (14, 13) (14, 14) (14, 15) (14, 16) (14, 17) (14, 18) (14, 19) (14, 20) (14, 21) (14, 22) (14, 23) (14, 24) (14, 25)
 (15, 00) (15, 01) (15, 02) (15, 03) (15, 04) (15, 05) (15, 06) (15, 07) (15, 08) (15, 09) (15, 10) (15, 11) (15, 12) (15, 13) (15, 14) (15, 15) (15, 16) (15, 17) (15, 18) (15, 19) (15, 20) (15, 21) (15, 22) (15, 23) (15, 24) (15, 25)

ps。根据ADyson的评论,如果您首先创建一个数组,然后想要循环遍历内容,则只会稍微调整输出数据的方法,就像这样:

void Main()
{
    var data = new char[,] {{'A','B','C'}, {'D','E','F'}};
    for(var row=0; row<data.GetLength(0); row++)
    {
        for(var col=0; col<data.GetLength(1); col++)
        {
            Console.Write(data[row,col]);
        }
        Console.WriteLine();
    }
}

您可以使用我们在原始帖子中使用的代码填充2D数组,仅像这样分配数组:

static void Main(string[] args)
{
    var numberOfRows = 16;
    var numberOfCols = 26;
    var data = new char[numberOfRows, numberOfCols];
    for (var currentRow = 0; currentRow < numberOfRows; currentRow++)
    {
        for (var currentCol = 0; currentCol < numberOfCols; currentCol++)
        {
            data[currentRow, currentCol] = Convert.ToChar(65 + currentCol);
        }
    }
}

另一种选择是将IEnumerable集合与lambda表达式和foreach循环一起使用:

void Main()
{

    //populate
    var data = Enumerable.Range(1,16).Select(x => 
        Enumerable.Range(1,26).Select(y => 
            Convert.ToChar(64 + y)
        )
    );

    //output one way
    Console.WriteLine("\r\nMethod 1\r\n");    
    foreach(var row in data)
    {
        foreach(var cell in row)
        {
            Console.Write(cell);
        }
        Console.WriteLine();
    }

    //or another
    Console.WriteLine("\r\nMethod 2\r\n");    
    data.ToList().ForEach(row => {
        row.ToList().ForEach(cell => 
            Console.Write(cell)
        ); 
        Console.WriteLine();
    });

    //or another approach convert each row from a collection of chars to strings separated by spaces, then join all the strings(rows) to one string separated by line breaks
    Console.WriteLine("\r\nMethod 3\r\n");    
    var output = string.Join("\r\n", data.Select(row => 
        string.Join(" ", row.Select(cell => 
            cell.ToString()
        ))
    )); 
    Console.WriteLine(output);
}

希望其中一些技巧可以帮助您提出一些想法。如果您需要任何说明或在实现方案时遇到任何问题,请在评论中说,我将根据需要提供其他帮助。


NB:解决任何问题的一个好方法是将其分解为多个步骤,因此,如果您希望围绕数据绘制网格,而不是用一种方法来完成所有工作,则为每个方法创建不同的功能您想要达到的目标;即,一种用于按需设置每一行的格式,另一种用于将这些行放入表格中,依此类推

下面是一个示例:https://dotnetfiddle.net/Tb0zkV

void Main()
{

    //populate
    var data = Enumerable.Range(1,16).Select(x => 
        Enumerable.Range(1,26).Select(y => 
            Convert.ToChar(64 + y)
        )
    );
    //display
    Console.WriteLine(data.ToTableFormatString());
}

public static class TableCreationExtension
{

    //converts the output of ToTableFormat to a single string
    public static string ToTableFormatString(this IEnumerable<IEnumerable<char>> rows) =>
        string.Join("\r\n", rows.ToTableFormat());

    /* 
        calls other methods to convert the data to the inner table format, then surrounds with a double edged box
        ╔═════╗
        ║inner║
        ║table║
        ║goes ║ 
        ║here ║
        ╚═════╝
        info on required characters here: https://en.wikipedia.org/wiki/Box-drawing_character           
    */
    public static IEnumerable<string> ToTableFormat(this IEnumerable<IEnumerable<char>> rows)
    {
        var rowsAsStrings = rows.ToInnerTableFormat();
        var rowLength = rowsAsStrings.First().Length; //assume that all rows are the same length, so the length of the first equals the length of all others
        //first row of box
        yield return string.Format("╔{0}╗", new string('═', rowLength));
        //each row within the box
        foreach(var row in rowsAsStrings)
        {
            yield return string.Format("║{0}║", row);
        }
        //last row of box
        yield return string.Format("╚{0}╝", new string('═', rowLength));
    }

    /*
        calls other methods to convert each row into a formatted string
        Adds a separator between each row.

        each
        ---------
        row
        ---------
        separated
    */
    public static IEnumerable<string> ToInnerTableFormat(this IEnumerable<IEnumerable<char>> rows)
    {
        bool returnInnerLine = false;
        int rowLength = 0; //assume all rows are the same length
        foreach(var row in rows)
        {
            var formattedRow = row.ToFormattedRow();
            if (returnInnerLine)
            {
                yield return new string('-', rowLength); //returns a line of -s the same length as a row for a between-row separator
            }
            else
            {
                returnInnerLine = true;
                rowLength = formattedRow.Length;
            }
            yield return formattedRow;
        }
    }

    //converts `'A', 'B', 'C'` to "A|B|C" 
    public static string ToFormattedRow(this IEnumerable<char> cellsInRow) =>
        string.Join("|", cellsInRow.Select(c => c.ToString()));
}

输出

+---------------------------------------------------+
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
¦---------------------------------------------------¦
¦A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z¦
+---------------------------------------------------+