如何打印二维数组中的行数?
How do I print the number of a row in a two dimensional array?
我正在用 C# 编写并尝试对打印数组中的行进行编号。我不知道如何打印二维数组。
打算这样写:
Row 0 # # # # # #
Row 1 # # # # # #
Row 2 # # # # # #
int[,] array1 = new int[6, 6]
{
{10, 20, 10, 20, 21, 99 },
{2, 27, 5, 45, 20, 13 },
{17, 20, 20, 33, 33, 20 },
{21, 35, 15, 54, 20, 37 },
{31, 101, 25, 55, 26, 66 },
{45, 20, 44, 12, 55, 98 }
};
int Length = array1.GetLength(0);
int Height = array1.GetLength(1);
Console.WriteLine(" Col 0 Col 1 Col 2 Col 3 Col 4 Col 5");
for (int i = 0; i < Length; i++)
{
for (int j = 0; j < Height; j++)
{
Console.Write(string.Format("{0,6} ", array1[i, j]));
}
Console.Write("\n" + "\n");
}
Console.ReadKey();
您只需在打印数组行的值之前写入控制台即可。
int[,] array1 = new int[6, 6]
{
{10, 20, 10, 20, 21, 99 },
{2, 27, 5, 45, 20, 13 },
{17, 20, 20, 33, 33, 20 },
{21, 35, 15, 54, 20, 37 },
{31, 101, 25, 55, 26, 66 },
{45, 20, 44, 12, 55, 98 }
};
int Length = array1.GetLength(0);
int Height = array1.GetLength(1);
Console.WriteLine(" Col 0 Col 1 Col 2 Col 3 Col 4 Col 5");
for (int i = 0; i < Length; i++)
{
Console.Write("Row {0} ", i); // Outside of the loop :)
for (int j = 0; j < Height; j++)
{
Console.Write(string.Format("{0,6} ", array1[i, j]));
}
Console.Write("\n" + "\n");
}
Console.ReadKey();
您想在第一个循环内但在第二个循环外写 Row 0
、Row 1
等,您应该使用 Console.Write()
,而不是 Console.WriteLine()
。
var array1 = new int[6, 6]
{
{10, 20, 10, 20, 21, 99 },
{2, 27, 5, 45, 20, 13 },
{17, 20, 20, 33, 33, 20 },
{21, 35, 15, 54, 20, 37 },
{31, 101, 25, 55, 26, 66 },
{45, 20, 44, 12, 55, 98 }
};
int Length = array1.GetLength(0);
int Height = array1.GetLength(1);
for (var i = 0; i < Length; i++)
{
Console.Write("Row {0} : ", i);
for (var j = 0; j < Height; j++)
{
Console.Write("{0,6} ", array1[i, j]);
}
Console.WriteLine();
}
请注意每个外循环迭代结束时的 Console.WriteLine()
,它将在下一次迭代之前将您带到新的一行。
输出
Row 0 : 10 20 10 20 21 99
Row 1 : 2 27 5 45 20 13
Row 2 : 17 20 20 33 33 20
Row 3 : 21 35 15 54 20 37
Row 4 : 31 101 25 55 26 66
Row 5 : 45 20 44 12 55 98
我正在用 C# 编写并尝试对打印数组中的行进行编号。我不知道如何打印二维数组。
打算这样写:
Row 0 # # # # # #
Row 1 # # # # # #
Row 2 # # # # # #
int[,] array1 = new int[6, 6]
{
{10, 20, 10, 20, 21, 99 },
{2, 27, 5, 45, 20, 13 },
{17, 20, 20, 33, 33, 20 },
{21, 35, 15, 54, 20, 37 },
{31, 101, 25, 55, 26, 66 },
{45, 20, 44, 12, 55, 98 }
};
int Length = array1.GetLength(0);
int Height = array1.GetLength(1);
Console.WriteLine(" Col 0 Col 1 Col 2 Col 3 Col 4 Col 5");
for (int i = 0; i < Length; i++)
{
for (int j = 0; j < Height; j++)
{
Console.Write(string.Format("{0,6} ", array1[i, j]));
}
Console.Write("\n" + "\n");
}
Console.ReadKey();
您只需在打印数组行的值之前写入控制台即可。
int[,] array1 = new int[6, 6]
{
{10, 20, 10, 20, 21, 99 },
{2, 27, 5, 45, 20, 13 },
{17, 20, 20, 33, 33, 20 },
{21, 35, 15, 54, 20, 37 },
{31, 101, 25, 55, 26, 66 },
{45, 20, 44, 12, 55, 98 }
};
int Length = array1.GetLength(0);
int Height = array1.GetLength(1);
Console.WriteLine(" Col 0 Col 1 Col 2 Col 3 Col 4 Col 5");
for (int i = 0; i < Length; i++)
{
Console.Write("Row {0} ", i); // Outside of the loop :)
for (int j = 0; j < Height; j++)
{
Console.Write(string.Format("{0,6} ", array1[i, j]));
}
Console.Write("\n" + "\n");
}
Console.ReadKey();
您想在第一个循环内但在第二个循环外写 Row 0
、Row 1
等,您应该使用 Console.Write()
,而不是 Console.WriteLine()
。
var array1 = new int[6, 6]
{
{10, 20, 10, 20, 21, 99 },
{2, 27, 5, 45, 20, 13 },
{17, 20, 20, 33, 33, 20 },
{21, 35, 15, 54, 20, 37 },
{31, 101, 25, 55, 26, 66 },
{45, 20, 44, 12, 55, 98 }
};
int Length = array1.GetLength(0);
int Height = array1.GetLength(1);
for (var i = 0; i < Length; i++)
{
Console.Write("Row {0} : ", i);
for (var j = 0; j < Height; j++)
{
Console.Write("{0,6} ", array1[i, j]);
}
Console.WriteLine();
}
请注意每个外循环迭代结束时的 Console.WriteLine()
,它将在下一次迭代之前将您带到新的一行。
输出
Row 0 : 10 20 10 20 21 99
Row 1 : 2 27 5 45 20 13
Row 2 : 17 20 20 33 33 20
Row 3 : 21 35 15 54 20 37
Row 4 : 31 101 25 55 26 66
Row 5 : 45 20 44 12 55 98