2017-10-16 48 views
-2

我被困在這個大學鍛鍊了一個星期,我無法想象如何解決它。如何根據用戶輸入將數組劃分爲二維數據?

該練習由用戶寫詞和存儲在數組上。然後,用戶輸入一個數字,程序根據輸入的用戶號碼將單詞數組分成一個二維數組。

例如:用戶寫入"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(); 
     } 
    } 
} 
+7

你在哪裏卡住到底是什麼?這似乎很有前途。發佈你試過的(代碼)或精確的問題,而不是整個任務。畢竟,任務是在那裏爲你的做法不驗證別人的解決方案;) – DanteTheSmith

+2

告訴我們你有什麼。如果我們爲你做你的功課,它會幫助任何人。不過,我們可以提供幫助。 – Amy

+0

您可以使用linq方法skip()和take()將數組分割成幾部分。 – jdweng

回答

1

嘗試使用Linq

using System.Linq; 

... 

// Let user input all the items in one go, e.g. 
// Car, Truck, Motorbike, Cat, Dog, Bird 
string[] source = Console 
    .ReadLine() 
    .Split(new char[] { ' ', '\t', ';', ',' }, StringSplitOptions.RemoveEmptyEntries); 

// size of the line; 
// simplest approach (Parse); int.TryParse is a better choice 
int n = int.Parse(Console.ReadLine()); 

// Let's create a jagged array with a help of modulo arithmetics: 
// source.Length/n + (source.Length % n == 0 ? 0 : 1) 
// we have "source.Length/n" complete lines and (possible) incomplete tail 
string[][] result = Enumerable 
    .Range(0, source.Length/n + (source.Length % n == 0 ? 0 : 1)) 
    .Select(index => source 
    .Skip(n * index) // skip index lines (each n items long) 
    .Take(n)   // take up to n items 
    .ToArray())  // materialize as array 
    .ToArray(); 

// finally, let's join the jagged array (line by line) into a single string 
string text = "[" + string.Join(" ", result 
    .Select(line => $"[{string.Join(", ", line)}]")) + "]"; 

Console.WriteLine(text); 

收入:

Car, Truck, Motorbike, Cat, Dog, Bird 
4 

結果:

[[Car, Truck, Motorbike, Cat] [Dog, Bird]] 
0

這裏是你想要做什麼的一般方法:

public static T[][] SplitArray<T>(T[] array, int size) { 
    // calculate how long the resulting array should be. 
    var finalArraySize = array.Length/size + 
     (array.Length % size == 0 ? 0 : 1); 
    var finalArray = new T[finalArraySize][]; 
    for (int i = 0 ; i < finalArraySize ; i++) { 
     // Skip the elements we already took and take new elements 
     var subArray = array.Skip(i * size).Take(size).ToArray(); // Take actually will return the rest of the array instead of throwing an exception when we try to take more than the array's elements 
     finalArray[i] = subArray; 
    } 
    return finalArray; 
}