存储二维字符串数组但显示一维字符串数组

时间:2016-11-16 18:47:41

标签: c# arrays string multidimensional-array 2d

我是一名相对较新的C#编程学生,我在使用2-D阵列方面遇到了很多麻烦。在这个程序中,用户应该为insult生成器输入名称,动词和对象,但问题是我必须毫无意义地将它们全部存储在一个二维字符串数组中并在一维字符串数组中生成侮辱我完全迷失了。我可以将名称,动词和对象存储到单个二维字符串数组中吗?任何帮助将不胜感激。

我的问题是初始化和存储2D字符串数组,然后转换为1D数组。

 private static void Generate(int insultnum, int sentnum)
    {
        int Ncounter = 1;
        int Vcounter = 1;
        int Ocounter = 1;
        string[,] name = new string[sentnum,?];
        string[,] verb = new string[sentnum,?];
        string[,] insult = new string[sentnum,?];
        do 
        {
            Console.Write("Please enter name of #{0}: ", Ncounter);
            //length and height 2D array for loop text is just a placeholder from an earlier project
            for (int i = 0; i < length; i++)
            {
                for (int j = 0; j < height; j++)
                {
                  name[Ncounter - 1, ??] = Console.ReadLine();

                }
            }
            //
            Ncounter++;
        } while (Ncounter < sentnum);
        do
        {
            Console.Write("Please enter name of #{0}: ", Vcounter);
            verb[Vcounter-1, ?] = Console.ReadLine();
            //2D array loop text
            Vcounter++;

        } while (Vcounter < sentnum);
        do
        {
            Console.Write("Please enter name of #{0}: ", Ocounter);
            insult[Ocounter-1, ?] = Console.ReadLine();
            //2D array loop text
            Ocounter++;

        } while (Ocounter < sentnum);
        Ncounter = 0;
        Vcounter = 0;
        Ocounter = 0;
        string[] insults = new string[insultnum];
        //insults = name[?,?] + " " + verb[?,?] + " " + object[?,?];
    }

示例输出:

  

输入要产生的侮辱次数:3
  输入名称,动词和对象的数量:3
  请输入名称#1:鲍勃
  请输入名称#2:Rhys
  请输入名称#3:Kaa
  请输入动词#1:licks
  请输入动词#2:触摸
  请输入动词#3:品味
  请输入对象#1:污垢
  请输入对象#2:汽车
  请输入对象#3:沥青
  已经产生了侮辱

     

Kaa舔污垢   鲍勃尝尝沥青   鲍勃舔车

1 个答案:

答案 0 :(得分:0)

2D数组,即string[,]是用于存储数据的奇怪集合,因为它需要

 NameCounter == VerbCounter == ObjectCounter 

更自然的选择是锯齿状的数组string[][],它允许任意 NameCounterVerbCounterObjectCounter。并且,请分解您的解决方案,将其拆分为 easy 的数量以实现和测试方法:

 // Random generator for insulting
 private Random rgen = new Random();

 // 1st index - category (name/verb/insult), 2nd index item within category
 private static string[,] data;

 private static string[] categories = new string[] {
   "name", "verb", "insult", 
 }; 

 // Input single categore, e.g. Names or Verbs 
 private static void InputCategory(int category) {
   for (int i = 0; i < data.GetLength(1); ++i) {
     Console.Write($"Please enter {categories[category]} #{i + 1}: ");
     data[category, i] = Console.ReadLine();
   }         
 }

 // Input all categories 
 private static void InputData() {
   Console.Write($"Enter the number of names, verbs, and objects: ");

   // simplest; more accurate implementation uses int.TryParse()
   int n = int.Parse(Console.ReadLine());

   data = new string[categories.Length, n];

   foreach(var int i = 0; i < categories.Length; ++i) 
     InputCategory();
 }

要编写侮辱生成器,您应该组合每个类别的随机项目

 private static string[] Insults(int count) {
   string[] result = new string[count];  

   // take count times a item from each category   
   for (int i = 0; i < count; ++i) {
     StringBuilder sb = new StringBuilder

     for (int c = 0; c < categories.Length; ++c) {
       if (c > 0)
         sb.Append(' ');

       sb.Append(data[c, rgen.Next(data.GetLength(1))]);  
     }

     result[i] = sb.ToString();
   } 

   return ressult; 
 } 

使用这些方法可以轻松放置

 private static void Main() {
   int numberOfInsults = ....

   InputData();

   foreach (var insult in Insults(numberOfInsults))
     Console.Write(insult);

   ...
 }