package com.company;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Created by ttc on 16-10-28.
*/
public class ShowWordsTimes
{
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+"次");
}
}
}
获取一段字符串中每个单词的次数(用空格分隔)
最新推荐文章于 2022-11-17 21:52:50 发布