【问题标题】:Store a 2-D string array but display a 1-D string array存储二维字符串数组但显示一维字符串数组
【发布时间】:2016-11-16 18:47:40
【问题描述】:

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

我的问题是初始化和存储二维字符串数组,然后转换为一维数组。

 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:舔
请输入动词 #2:touches
请输入动词 #3:味道
请输入对象 #1:污垢
请输入对象 #2:汽车
请输入对象 #3:沥青
已经产生了侮辱

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

【问题讨论】:

  • 你为什么使用二维数组?
  • 因为除了我的老师告诉我之外没有其他原因。

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


【解决方案1】:

二维数组string[,]是一个有点奇怪的集合来存储数据,因为它需要

 NameCounter == VerbCounter == ObjectCounter 

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

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

   ...
 } 

【讨论】:

  • 非常感谢您的回复!我看到您只是将二维数组视为图形,然后从那里开始。我们对 stringbuilder 的了解还不够,但非常感谢您
猜你喜欢
  • 2021-06-12
  • 1970-01-01
  • 2016-05-06
  • 2015-01-21
  • 2013-03-28
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 2021-07-10
相关资源
最近更新 更多