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

本文关键字:字符串 数组 显示 一维 二维 存储 | 更新日期: 2023-09-27 18:18:19

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

我的问题是初始化和存储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:Bob
请输入姓名#2:Rhys
请输入名称#3:Kaa
请输入动词#1:links
请输入动词#2:触摸
请输入动词#3:tastes
请输入对象#1:污垢
请输入对象#2:cars
请输入对象#3:沥青
侮辱已经产生

Kaa舔泥土
鲍勃品尝沥青
Bob舔汽车

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

2D数组string[,]是位奇怪集合来存储数据,因为它要求

 NameCounter == VerbCounter == ObjectCounter 

更自然的选择是一个锯齿形数组string[][],它允许任意 NameCounter, VerbCounter, ObjectCounter。并且,请分解您的解决方案,将其拆分为易于实现和测试方法的数量:

 // 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);
   ...
 }