我需要将此表中的数据放入数组中,然后在控制台中将数组打印为格式化表。 这是我从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();
}
}
}
任何想法?
答案 0 :(得分: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();
}
答案 1 :(得分:0)
如果您在控制台中使用等宽字体,则可以通过在必要时添加更多空格来调整每件事物的显示位置
例如,对应于第1行和第2行以及第2列第1列的成员,这将是要计算的内容:
最大的单词是星期三是9个字母,在第一行的第一列我应该放9个espaces,因为会有一个空白。然后你可以在列之间放置四个空格作为分隔符,然后对于第二列,你计算出1:00是最大的字符串,所以对于12你要添加2个额外的空格,依此类推。
使用制表符而不是某些空格也可能有用,但如果表格的某些字符串比另一列中的字符串大得多,则无效。
希望它有所帮助。
答案 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();
}
}
}
}