得到输入从用户和创建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

为什么妳说其复杂? 任务是采取2D分钱阵列从控制台。 什么错?
Arie

也许"复杂"是错误的词。 但根据使用情况,有更好的替代办法比使用2 1D阵列作为一种缓冲器,以创建一个2D阵列。
Axs

其他语言

此页面有其他语言版本

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