을 얻을 입력에서 사용자 만들 2D 배열

0

질문

고 싶을 입력에서 사용자 만들 2D 배열하는 방식 사용자의 입력 값에서 두 개의 노선이 있습니다. 첫 번째 사용자 정의 값을 공백으로 구분하여 다중 입력하고 다른 값은 또한 분리된 공간을 아래와 같은 예제:

게 값:

2 3 4
5 6 7

변수가 있어야이에서 끝:

[[2, 3, 4], [5, 6, 7]]

또 다른 예를 들어:

게 값:

1 2
3 4

변수가 있어야이에서 끝:

[[1, 2], [3, 4]]
c#
2021-11-24 06:06:06
1

최고의 응답

2

나는 정직하지 못하는 이유를 알고있는 것 그것이 복잡,하지만 당신은 여기 이동:

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();
}

는 경우 지정할 수 있습니다 당신의 활용 사례 난데없이 당신을 도울 수 있는 방법을 더 나은 솔루션입니다.

2021-11-24 06:43:48

u 는 말했다가 복잡해? 는 것입니다 작업을 2D 한푼에서 배열 console. 무엇입니까?
Arie

어쩌면"복잡는"잘못된 단어입니다. 지에 따라 사용-경우,수있는 더 나은 대안을 사용하는 것보다 2 1D 배열의 일종으로 버퍼를 만드는 2D 배열입니다.
Axs

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................