如何打印C#中用逗号分隔的多维数组值
如何在打印数组时添加大括号,如数组初始化声明
发布于 2022-02-28 10:32:12
int[,] array_1 = new int[3, 6]
{
{12,12,23,34,34,4 },
{32,43,12,45,34,4 },
{54,21,12,43 ,34,4}
};
for (int i = 0; i < array_1.GetLength(0); i++)
{
int j = 0;
for (j = 0; j < array_1.GetLength(1); j++)
{
if (j == 0)
{
Console.Write("{");
}
Console.Write(array_1[i, j]);
if (array_1.GetLength(1) > j + 1)
{
Console.Write(", ");
}
}
Console.Write("}");
Console.WriteLine();
}
Console.ReadLine();发布于 2022-02-28 10:45:29
您可以像这样打印2D数组:
for (int i = 0; i < array_1.GetLength(0); i++)
{
// unconditionally start every line with a {
Console.Write("{");
// print comma separated except the last element
for (int j = 0; j < array_1.GetLength(1)-1; j++)
{
Console.Write(array_1[i, j]);
Console.Write(", ");
}
// last element
Console.Write(array_1[i, array_1.GetLength(1)-1]);
// unconditionally end every line with a }
Console.WriteLine("}");
}https://stackoverflow.com/questions/71293600
复制相似问题