我被困在这个大学锻炼了一个星期,我无法想象如何解决它。如何根据用户输入将数组划分为二维数据?
该练习由用户写词和存储在数组上。然后,用户输入一个数字,程序根据输入的用户号码将单词数组分成一个二维数组。
例如:用户写入"Car", "Truck", "Motorbike", "Cat", "Dog", "Bird"
。并提出"3"
,所以程序在此:
["Car", "Truck", "Motorbike", "Cat", "Dog", "Bird"]
到
[["Car", "Truck", "Motorbike"] ["Cat", "Dog", "Bird"]]
而且,如果用户输入4
,返回必须是:
[["Car", "Truck", "Motorbike", "Cat"] ["Dog", "Bird"]]
编辑:下面的代码
using System;
using System.Collections;
namespace probando_separar_arrays {
class Program {
static void Main(string[] args) {
int num, i = 0;
String pos;
ArrayList array = new ArrayList();
do {
Console.Write("Please write a word: ");
pos = Console.ReadLine();
array.Add(pos);
} while (!int.TryParse(pos, out num));
Console.WriteLine("The input words are: ");
while (i < array.Count - 1) {
Console.WriteLine(array[i]);
i++;
}
/* Here is where I got stuck, cannot find a way to initialize the
* Bidimensional array
*/
Console.ReadKey();
}
}
}
你在哪里卡住到底是什么?这似乎很有前途。发布你试过的(代码)或精确的问题,而不是整个任务。毕竟,任务是在那里为你的做法不验证别人的解决方案;) – DanteTheSmith
告诉我们你有什么。如果我们为你做你的功课,它会帮助任何人。不过,我们可以提供帮助。 – Amy
您可以使用linq方法skip()和take()将数组分割成几部分。 – jdweng