C# 每次迭代都使用C
可能重复: 嗯,我在我的代码的这一棘手的部分,并被困在目前,所以我要求一些帮助。我也在用C语言开发这个。下面是我的一段代码,该代码将处理它: 显示的方法就是我遇到的问题,其他人都是对的。问题输出是错误的,与for-each循环有关。请看一下我的输出现在看起来像什么以及我想让它看起来像什么的链接C# 每次迭代都使用C,c#,C#,可能重复: 嗯,我在我的代码的这一棘手的部分,并被困在目前,所以我要求一些帮助。我也在用C语言开发这个。下面是我的一段代码,该代码将处理它: 显示的方法就是我遇到的问题,其他人都是对的。问题输出是错误的,与for-each循环有关。请看一下我的输出现在看起来像什么以及我想让它看起来像什么的链接 thanks alot guys the question was answered 在选择中使用.Distinct: var items=按对字典顺序从对开始。值降序选择对。不同 将显示方法中的代码
thanks alot guys the question was answered
在选择中使用.Distinct: var items=按对字典顺序从对开始。值降序选择对。不同
将显示方法中的代码替换为类似的内容将返回所需结果。以下是获得所需结果的查询
var dict = items.GroupBy(x=>x.Value).ToDictionary(y=> y.Key, y=> String.Join(" ", y.Select(z=>z.Key)));
要理解上述查询,请参阅和
以下是您修改的程序
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();
}
@西蒙怀特黑德:我在找那个职位;普罗蒂普:不要问重复的问题。你会被骂的。不过,还是要用原始帖子把它分解出来,直到它按照你想要的方式工作。在@SimonWhitehead链接的另一个问题中,你没有问关于按特定标准对项目进行分组的问题,只是为了在列中显示它们。现在,他要求将这两个项目进行分组,并在列中显示它们。我认为您需要按照此处所述对项目进行分组:然后将您在上一个问题的答案中得到的内容呈现给他们。@AustinSmith您仍然在询问每行问题的单词。你试过我的答案了吗?因为你刚才在回答这个问题时问了我的链接解决方案提供了什么……非常感谢,但现在它看起来像是@AustinSmith,因此,我使用了“相似”这个词。如果您希望应用更精确的格式,那么我的目标只是指出它写入错误的原因是Console.WriteLineWords occuring+i.ToString+times的位置;在您的foreach循环中。@AustinSmith在那里编辑应该应用您正在寻找的格式。哦,好的,非常感谢我将继续工作以获得正确的格式,但感谢您给我一个想法上帝保佑您的帮助再次感谢您它仍然没有获得正确的格式我将继续工作以获得正确的格式但是谢谢你给我一个主意上帝保佑你的帮助哇太感谢了现在输出看起来是这样的你是否碰巧知道如何放置空格以便一行只有四个单词这有点棘手,你需要替换字符串。加入类似的东西,在这个例子中,每第四个单词放置一行。
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