我想要输入从用户和创建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();
}
如果你能指定您的使用情况,我敢肯定我可以帮你拿出一个方法更好的解决方案。