C #으로 작성 중이며 인쇄 된 배열의 행에 번호를 지정하려고합니다. 2 차원 배열을 인쇄하는 방법을 사용하는 방법에 대한 단서가 없습니다.
다음과 같이 작성합니다.
행 0 # # # # # #
행 1 # # # # # #
행 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();
Row 0
, Row 1
etc를 첫 번째 루프 내부 에 작성하고 두 번째 루프 외부 에 작성하려면 , 사용 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()
다음 반복 전에 새 라인에 당신을 데려 갈 것이다 각각의 외부 루프 반복의 끝에서.
행 0:10 20 10 20 21 99
1 열 : 2 27 5 45 20 13
2 열 : 17 20 20 33 33 20
3 열 : 21 35 15 54 20 37
4 열 : 31101 25 55 26 66
5 열 : 45 20 44 12 55 98
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다