我是编程新手,我正在尝试编写一个程序来接收字符串数组(数组的每个索引都是一个单词),然后计算字符串中每个单词的出现次数。这是我到目前为止所拥有的:
string[] words =
{
"which",
"wristwatches",
"are",
"swiss",
"wristwatches"
};
Array.Sort (words);
for (int i = 0; i < words.Length; i++)
{
int count = 1;
for(int j = 1; j < words.Length; j++)
{
if (words [i] == words [j])
{
count++;
}
}
Console.WriteLine ("{0} {1}", words[i], count);
}
理想情况下,我希望输出类似于:
是 1
瑞士 1
其中 1
手表 2