2017-10-16 117 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; 
}