在我的代码这个棘手的部分以及IM和被卡住暂且这么即时通讯寻求一些帮助。我正在用C#开发它。这里是我的代码片,它的交易: 方法显示是即时通讯对其他人的问题是正确的。问题输出是错误的,并且与每个循环有关。请看看我的什么输出看起来像现在,什么IM的链接试图使它看起来像请
thanks alot guys the question was answered
在我的代码这个棘手的部分以及IM和被卡住暂且这么即时通讯寻求一些帮助。我正在用C#开发它。这里是我的代码片,它的交易: 方法显示是即时通讯对其他人的问题是正确的。问题输出是错误的,并且与每个循环有关。请看看我的什么输出看起来像现在,什么IM的链接试图使它看起来像请
thanks alot guys the question was answered
在选择使用.Distinct():
var items = (from pair in dictionary order by pair.Value descending select pair).Distinct();
没有没有做任何事情 –
var max =
(from pair in dictionary
select pair.Value).Max()
for (int i = max; i > -1; i--)
{
var items =
from pair in dictionary
where pair.Value == i
select pair.Key;
if (items.Count() > 0)
{
Console.WriteLine("\nWords occuring " + i.ToString() +" times");
int count = 0;
foreach(var item in items)
{
if (count == 4)
{
Console.WriteLine("");
count = 0;
}
else
{
count++;
}
Console.Write(item + "\t");
}
}
}
用类似于此的替换display
方法中的代码应返回所需的结果。
非常感谢,但现在它看起来像这样http://imageshack.us/photo/my-images /443/41895266.png/ –
@奥斯汀史密斯因此,我使用了类似的词。如果你希望更精确地应用格式,我的目标只是指出它写错的原因是放置了'Console.WriteLine(“Words occuring”+ i.ToString()+“times”);''在你的foreach循环中。 –
@AustinSmith那里的编辑应该适用你正在寻找的格式。 –
以下是查询以获得期望的结果
var dict = items.GroupBy(x=>x.Value).ToDictionary(y=> y.Key, y=> String.Join(" ", y.Select(z=>z.Key)));
要理解上面的查询,请参阅grouping,ToDictionary和String.Join。
以下是您所修改的程序
void Main()
{
SortedDictionary<string, int> dict =Words();
display(dict);
Console.WriteLine();
}
private static SortedDictionary<String, int> Words()
{
SortedDictionary<string, int> dic = new SortedDictionary<string, int>();
String input = "today is Wednesday right and it sucks. today how are you are you a rabbit today";
string[] word = Regex.Split(input, @"\s");
foreach (string current in word)
{
string wordKey = current.ToLower();
if (dic.ContainsKey(wordKey))
{
++dic[wordKey];
}
else
{
dic.Add(wordKey, 1);
}
}
return dic;
}
private static void display(SortedDictionary<string, int> dictionary)
{
var items = from pair in dictionary
orderby pair.Value descending
select pair;
var dict = items.GroupBy(x=>x.Value).ToDictionary(y=> y.Key, y=> String.Join(" ", y.Select(z=>z.Key)));
foreach (var item in dict)
{
Console.WriteLine("Words occurung "+item.Key +" times");
Console.WriteLine("{0}", item.Value);
}
Console.ReadLine();
}
输出
Words occurung 3 times
today
Words occurung 2 times
are you
Words occurung 1 times
a and how is it rabbit right sucks. wednesday
哇感谢这么多现在输出看起来像这样http://imageshack.us/a/img198/3569/44164476.png你会发生吗?知道如何放置空格,以便只有四个单词在一条线上 –
它有点棘手,您需要用类似的东西替换String.Join,为每n(本例中为第4)个单词放置一个新的线。 – Tilak
@SimonWhitehead我一直在寻找该职位;);) – Hardrada
普罗蒂普:不要问重复的问题。你会被炸毁。 –
你的方式不对 –