2D array to table in c#?

前端 未结 3 1053
渐次进展
渐次进展 2021-01-29 10:36

I need to put the data from this table into an array and then make the array print as a formatted table in the console. Here\'s the table that I got my data from http://puu.sh/

相关标签:
3条回答
  • 2021-01-29 10:59

    Got it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Zumba1
    {
        class Zumba
        {
            static void Main(string[] args)
            { //Recreated the data in the table for the zumba section, added each row, and each column. Worked on formatting.
                string[,] schedule = new string[8, 6] { { "\t\t1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                     {"Monday", "\t12", "10", "17", "22", "$244",   },
                                     {"Tuesday", "\t11", "13", "17", "22", "$252",},
                                     {"Wednesday", "12", "10", "22", "22", "$264",},
                                     {"Thursday", "9", "14", "17", "22", "$248",},
                                     {"Friday", "\t12", "10", "21", "12", "$220",},
                                     {"Saturday", "12", "10", "5", "10", "$148"},
                                     {" ", " ", " ", " ", " ","\t$1376",}};
                //Nested for loops to print in a table-style format.
                for (int i = 0; i < schedule.GetLength(0); i++)
    
                {
                    for (int j = 0; j < schedule.GetLength(1); j++)
                    {
                        Console.Write(schedule[i, j] + "\t");
                    }
                    {
                        Console.WriteLine(i.ToString());
                    }
    
                }
         Console.ReadLine();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 11:09

    Foreach on a [,] array gives you all elements as a list, as you noticed. In this case you need to output as follow:

    for (int x0 = 0; x0 < schedule.GetLength(0); x0++)
    {
        for (int x1 = 0; x1 < schedule.GetLength(1); x1++)
        {
            Console.Write("{0}\t", schedule[x0, x1]);
        }
        Console.WriteLine();
    }
    Console.ReadKey();
    

    If you want to use foreach for any reason, you can also declare your table as [][] array. But in both ways you have to create 2 loops:

    string[][] schedule = new string[][] {
                                        new string[] { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                        new string[] {"Monday", "12", "10", "17", "22", "244",   },
                                        new string[] {"Tuesday", "11", "13", "17", "22", "252",},
                                        new string[] {"Wednesday", "12", "10", "22", "22", "264",},
                                        new string[] {"Thursday", "9", "14", "17", "22", "248",},
                                        new string[] {"Friday", "12", "10", "21", "12", "220",},
                                        new string[] {"Saturday", "12", "10", "5", "10", "148"},
                                        new string[] {" ", " ", " ", " ", " ","1376",}
            };
    foreach (string[] line in schedule)
    {
        foreach (string i in line)
            Console.Write("{0}\t", i);
        Console.WriteLine();
    }
    
    0 讨论(0)
  • 2021-01-29 11:15

    If you use a monospaced font in the console you can adjust where each thing displays by putting more spaces if necessary

    For example for the members corresponding to the the 1st and 2nd row and 1st of second column this would be the thing to calculate:

    Largest word is Wednesday that is 9 letters, on first row first column I should put 9 espaces as there would be a blank. Then you might put four spaces as a separator between columns, then for the second column you calculate that 1:00 is the largest string so for 12 you would add 2 extra spaces, and so on.

    Using a tab instead of some spaces is also likely to work but if the table ends up having some string that is much larger than one in another column it won't work.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题