关于c#的一个问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 13:50:39
它是一个让控制台输入的字符串
然后可以算出每个字出现的次数
用Foreach和if else 和for循环

以下给出的是在main里面的方法,已经通过了测试,然后命名空间用默认的就可以了(using System;using System.Collections.Generic;)
string s = Console.ReadLine();
Dictionary<char, int> map = new Dictionary<char, int>();
foreach (char c in s)
{
bool bFlag = false;
foreach (KeyValuePair<char, int> pair in map)
{
if (pair.Key == c)
{
bFlag = true;
}
}
if (bFlag)
{
map[c]++;
}
else
{
map.Add(c, 1);
}
}
foreach (KeyValuePair<char,int> c in map)
{
Console.WriteLine("字符:" + c.Key.ToString() + ":"