【发布时间】:2019-09-16 17:23:19
【问题描述】:
我正在编写代码来计算数组中每个单词出现的次数以及出现的所有单词的总数。我设法创建了一个数组,允许用户添加他们希望检查的多个单词。但是,我正在努力寻找一种方法来单独计算每个单词在句子中出现的次数(我只能让它对数组的第一个元素起作用)。
我尝试了一个 for 循环,完成后它将移动到数组中的下一个元素,但它不会为下一个元素再次开始 for 循环,而是结束代码块。
int occurences = 0;
string[] words = new string[_wordCount];
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine("Type in the censored words you wish to be counted: ");
words[i] = Console.ReadLine();
if (_sentence.Contains(words[i]))
{
occurences++;
}
if (i > words.Length)
{
i++;
}
}
Console.WriteLine("Number of censored word occurences: " + occurences);
return occurences;
【问题讨论】:
标签: c#