簡體   English   中英

二維數組到C#中的表格?

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

我需要將這個表中的數據放入一個數組中,然后將數組打印為控制台中的格式化表。 這是我從http://puu.sh/oqV8f/7d982f2665.jpg 獲取數據的表格; 我只需要讓數組輸出行和列而不是列表。 到目前為止我有這個:

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

有任何想法嗎?

正如您所注意到的,[,] 數組上的Foreach將所有元素作為列表提供。 在這種情況下,您需要輸出如下:

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

如果出於任何原因要使用foreach ,也可以將表聲明為 [][] 數組。 但在這兩種方式中,您都必須創建 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();
}

如果您在控制台中使用等寬字體,您可以在必要時通過放置更多空格來調整每個事物的顯示位置

例如,對於對應於第一行和第二行以及第二列第一行的成員,這將是要計算的事情:

最大的詞是星期三,它是 9 個字母,在第一行第一列我應該放 9 個空格,因為會有一個空格。 然后您可以在列之間放置四個空格作為分隔符,然后對於第二列,您計算出 1:00 是最大的字符串,因此對於 12,您將添加 2 個額外的空格,依此類推。

使用制表符代替某些空格也可能有效,但如果表格最終包含的字符串比另一列中的字符串大得多,則它將不起作用。

希望能幫助到你。

知道了。

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