고 싶을 입력에서 사용자 만들 2D 배열하는 방식 사용자의 입력 값에서 두 개의 노선이 있습니다. 첫 번째 사용자 정의 값을 공백으로 구분하여 다중 입력하고 다른 값은 또한 분리된 공간을 아래와 같은 예제:
게 값:
2 3 4
5 6 7
변수가 있어야이에서 끝:
[[2, 3, 4], [5, 6, 7]]
또 다른 예를 들어:
게 값:
1 2
3 4
변수가 있어야이에서 끝:
[[1, 2], [3, 4]]
고 싶을 입력에서 사용자 만들 2D 배열하는 방식 사용자의 입력 값에서 두 개의 노선이 있습니다. 첫 번째 사용자 정의 값을 공백으로 구분하여 다중 입력하고 다른 값은 또한 분리된 공간을 아래와 같은 예제:
게 값:
2 3 4
5 6 7
변수가 있어야이에서 끝:
[[2, 3, 4], [5, 6, 7]]
또 다른 예를 들어:
게 값:
1 2
3 4
변수가 있어야이에서 끝:
[[1, 2], [3, 4]]
나는 정직하지 못하는 이유를 알고있는 것 그것이 복잡,하지만 당신은 여기 이동:
Console.Write("Please insert values separated by white-space: ");
string userInputLine1 = Console.ReadLine();
Console.Write("Please insert values seperated by white-space again: ");
string userInputLine2 = Console.ReadLine();
string[] userInputLine1Splitted = userInputLine1.Split(" ");
string[] userInputLine2Splitted = userInputLine2.Split(" ");
// Either this or catch an out-of-boundary exception when one is larger than the other and fill the space with 0's or something else.
if (userInputLine1Splitted.Length != userInputLine2Splitted.Length)
{
throw new Exception("Both 1d arrays need to be the same length!");
}
int lengthOfArray = userInputLine1Splitted.Length;
// Since we always have only 2 rows this can be hardcoded.
int[,] TwoDArrayFromUserInput = new int[2, lengthOfArray];
for (int col = 0; col < lengthOfArray; col++)
{
TwoDArrayFromUserInput[0, col] = Convert.ToInt32(userInputLine1Splitted[col]);
TwoDArrayFromUserInput[1, col] = Convert.ToInt32(userInputLine2Splitted[col]);
}
// Print to console to prove it worked.
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < lengthOfArray; col++)
{
Console.Write(TwoDArrayFromUserInput[row, col] + " ");
}
Console.WriteLine();
}
는 경우 지정할 수 있습니다 당신의 활용 사례 난데없이 당신을 도울 수 있는 방법을 더 나은 솔루션입니다.