日期:2014-05-20 浏览次数:20792 次
package cn.dzr.collection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class GetCharN
{
public static void main(String[] args) throws IOException
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String str=bufr.readLine();
/*(HashMap)从输入框输入一个字符串,要求统计每一个字符出现的频率,
* 并按照字母排 序之后输出.频率=字符出现的次数/字符总数*/
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
int len = str.length();
for(int i = 0;i<len; i++)
{
Character c = str.charAt(i);
if(map.containsKey(c))
{
map.put(c, 1+map.get(c));
}
else
{
map.put(c, 1);
}
}
for(Map.Entry<Character,Integer> entry:map.entrySet())
{
char c = entry.getKey();
int i = entry.getValue();
System.out.println(c+": "+(double)i/len);
}
}
}