您的位置:首页 > 其它

统计单词出现的次数。

2016-10-30 20:01 127 查看
Package  com.company;

Import  java.util.*;

Public class Mian{

     Public static void main(String[] args){

System.out.println(“请输入一段英文”);

Scanner sc=new Scanner(System.in);

String strPara =sc.nextLine();

String[] strArray =strPara.split(“ ”);

Map<String,Integer> mapWords =new HashMap<String,Integer>();

For(String str :strArray)

{

If(!mapWords.containsKey(str))

{

mapWords.put(str,1);

}

else

{

Integer iCount=mapWords.get(str);

iCount++;

mapWords.put(str,iCount);

}

}

For(Map.Entry<String,Integer>me:mapWords.entrySet())

{

String strKey=me.getKey();

Integer iCount=me.getValue();

System.out.println(strKey+”出现了”+iCount+”次”);

}

}

}

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: