C# 帮助我理解这个c代码

C# 帮助我理解这个c代码,c#,C#,此代码在 这是一个程序,让用户在多行文本框中输入几个句子,然后计算每个字母在该文本中出现的次数 private const int MAXLETTERS = 26; // Symbolic constants private const int MAXCHARS = MAXLETTERS - 1; private const int LETTERA = 65; private void btnCalc_Click(object sender, EventAr

此代码在

这是一个程序,让用户在多行文本框中输入几个句子,然后计算每个字母在该文本中出现的次数

  private const int MAXLETTERS = 26;            // Symbolic constants
  private const int MAXCHARS = MAXLETTERS - 1;
  private const int LETTERA = 65;

private void btnCalc_Click(object sender, EventArgs e)
  {
    char oneLetter;
    int index;
    int i;
    int length;
    int[] count = new int[MAXLETTERS];
    string input;
    string buff;

    length = txtInput.Text.Length;
    if (length == 0)    // Anything to count??
    {
      MessageBox.Show("You need to enter some text.", "Missing Input");
      txtInput.Focus();
      return;
    }
    input = txtInput.Text;
    input = input.ToUpper();


    for (i = 0; i < input.Length; i++)    // Examine all letters.
    {
      oneLetter = input[i];               // Get a character
      index = oneLetter - LETTERA;        // Make into an index
      if (index < 0 || index > MAXCHARS)  // A letter??
        continue;                         // Nope.
      count[index]++;                     // Yep.
    }

    for (i = 0; i < MAXLETTERS; i++)
    {
      buff = string.Format("{0, 4} {1,20}[{2}]", (char)(i + LETTERA)," ",count[i]);
      lstOutput.Items.Add(buff);
    }
  }
这行代码

buff = string.Format("{0, 4} {1,20}[{2}]", (char)(i + LETTERA)," ",count[i]);
这是一个后期增量。如果要保存该值的返回值,它将是增量之前的count[index],但它基本上只会增加值并返回增量之前的值。至于方括号内有变量的原因,它引用的是数组索引中的值。换句话说,如果你想谈论街道上的第五辆车,你可以考虑一些类似街车的东西。在C语言中,我们使用方括号和零索引,所以我们会有类似电车的东西[4]。如果有一个Car数组调用StreetCars,则可以使用索引值引用第5辆车

至于string.Format方法,请检查。

count[index]++;表示在索引处的计数值上加1。++被称为递增。代码所做的是统计字母出现的次数

buff=string.Format{0,4}{1,20}[{2}],chari+letta,count[i];正在格式化一行输出。使用,首先传入一个格式说明符,该说明符的工作方式类似于模板或格式字母。{和}之间的部分指定如何使用传递到string.Format的附加参数。让我详细分析一下格式规范:

{0, 4} The first (index 0) argument (which is the letter, in this case). The ,4 part means that when it is output, it should occupy 4 columns of text. {1,20} The second (index 1) argument (which is a space in this case). The ,20 is used to force the output to be 20 spaces instead of 1. {2} The third (index 2) argument (which is the count, in this case).
因此,当string.Format运行时,chari+letta被用作第一个参数,并插入到格式的{0}部分。插入{1},count[i]插入{2}。

根据Surfer513的评论,count是一个整数数组。数组就像火车上的汽车,每个都有一个值。因此,这段代码将一个字符转换为它的数字等价物,查找数组中的该位置,并将计数增加1,因为代码试图记录字母频率。我假设,由于他们没有质疑I++位,它要么混入,要么就是数组位。如果您愿意的话,请随时将这些内容包含在您的答案中wish@tarek11011是的,当然。“continue”的作用是在执行时移动到循环的下一个迭代。例如,如果您有continue,它将执行,即使它说Console.WriteLineThis将永远不会执行;在continue之后,WriteLine方法将不会执行,因为当到达continue时,它将进入循环的下一个迭代。有意义吗?@tarek11011,另一个是“break”。如果在一个循环中执行break,那么不管剩下多少次迭代或者循环中发生了什么,如果执行了break,那么循环就会立即终止。所以使用“break”退出循环。仅供参考。@tarek11011,一点问题也没有。软件开发是一件美好的事情。保持对它的热情,在你意识到它之前,你将用伟大的代码做一些非常了不起的事情。
count[index]++;
{0, 4} The first (index 0) argument (which is the letter, in this case). The ,4 part means that when it is output, it should occupy 4 columns of text. {1,20} The second (index 1) argument (which is a space in this case). The ,20 is used to force the output to be 20 spaces instead of 1. {2} The third (index 2) argument (which is the count, in this case).