获取一段字符串中每个单词的次数(用空格分隔)

来源:互联网 发布:软件超频卡死 编辑:程序博客网 时间:2024/06/02 05:31
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+"次");        }    }}

0 0
原创粉丝点击