2016-04-21 60 views
-1

我需要從這個表中的數據放入一個數組,然後在控制檯中將數組打印爲格式化的表。 下面是我從http://puu.sh/oqV8f/7d982f2665.jpg獲得數據的表格;我只需要使數組輸出行和列而不是列表。 我有這個至今:二維數組在c#中的表?

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

什麼想法?

+0

你在哪裏卡住了? – mbeckish

+0

你有一個二維數組,你想把它變成一張表?什麼樣的桌子? HTML表格?數據庫表? – dfdsfdsfsdf

+0

的可能的複製[格式化字符串轉換成列(http://stackoverflow.com/questions/2978311/format-a-string-into-columns) –

回答

1

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

如果您在控制檯中使用等寬字體可以調節每個東西顯示通過把更多的空間,如果有必要

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

最大的單詞是星期三,它是9個字母,在第一行第一列我應該放9個空格,因爲會有空白。然後你可以在列之間放置四個空格作爲分隔符,然後爲第二列計算1:00是最大的字符串,所以對於12,你會添加2個額外的空格,等等。

使用而不是一些空格的標籤也可能奏效,但如果表結束有一些字符串是超過一個在另一列是行不通大得多。

希望它有幫助。

0

明白了。

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