我被困在這個大學鍛鍊了一個星期,我無法想象如何解決它。如何根據用戶輸入將數組劃分爲二維數據?
該練習由用戶寫詞和存儲在數組上。然後,用戶輸入一個數字,程序根據輸入的用戶號碼將單詞數組分成一個二維數組。
例如:用戶寫入"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