Získajte vstup od užívateľa a vytvorenie 2D pole

0

Otázka

Chcem mať vstup od užívateľa a vytvorenie 2D pole tak, že užívateľ vstupy hodnoty v dvoch líniách. Prvý používateľ definovať hodnoty oddelené medzerami potom hity enter a dáva inej hodnoty aj oddelené priestory, ako je uvedené nižšie, na príklad:

Dať hodnoty:

2 3 4
5 6 7

variabilný toto by malo mať na konci:

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

Iný príklad:

Dať hodnoty:

1 2
3 4

variabilný toto by malo mať na konci:

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

Najlepšiu odpoveď

2

Úprimne neviem, prečo by ste, aby to komplikované, ale tu máš:

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

Ak by ste mohli zadať vaše používanie prípade som si istá, že som mohol pomôcť môžete prísť so spôsobom, ako lepšie riešenie.

2021-11-24 06:43:48

prečo u povedal jej komplikované? Úloh je, aby 2D dime pole z konzoly. Čo je na tom zlého?
Arie

Možno "komplikované" bolo zlé slovo. Ale v závislosti od použitia-prípad, tam sú oveľa lepšie alternatívy, ako pomocou 2 1D pole ako druh rezervy na vytvorenie 2D pole.
Axs

V iných jazykoch

Táto stránka je v iných jazykoch

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