简体   繁体   English

二维数组到C#中的表格?

[英]2D array to table in c#?

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/oqV8f/7d982f2665.jpg ;这是我从http://puu.sh/oqV8f/7d982f2665.jpg 获取数据的表格; I just need to make the array output rows and columns instead of a list.我只需要让数组输出行和列而不是列表。 I have this so far:到目前为止我有这个:

 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.
            string[,] schedule = new string [8, 6] { { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                 {"Monday", "12", "10", "17", "22", "244",   },
                                 {"Tuesday", "11", "13", "17", "22", "252",},
                                 {"Wednesday", "12", "10", "22", "22", "264",},
                                 {"Thursday", "9", "14", "17", "22", "248",},
                                 {"Friday", "12", "10", "21", "12", "220",},
                                 {"Saturday", "12", "10", "5", "10", "148"},
                                 {" ", " ", " ", " ", " ","1376",}};
            foreach (string i in schedule)
            {
                Console.WriteLine(i.ToString());
            }
            Console.ReadKey();
        }
    }
}

Any Ideas?有任何想法吗?

Foreach on a [,] array gives you all elements as a list, as you noticed.正如您所注意到的,[,] 数组上的Foreach将所有元素作为列表提供。 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.如果出于任何原因要使用foreach ,也可以将表声明为 [][] 数组。 But in both ways you have to create 2 loops:但在这两种方式中,您都必须创建 2 个循环:

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

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.最大的词是星期三,它是 9 个字母,在第一行第一列我应该放 9 个空格,因为会有一个空格。 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.然后您可以在列之间放置四个空格作为分隔符,然后对于第二列,您计算出 1:00 是最大的字符串,因此对于 12,您将添加 2 个额外的空格,依此类推。

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.希望能帮助到你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM