package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
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)
{
//如果map当前中不包含该单词,那么将该单词加入到map集合中,该单词作为key,值设为1
if(!mapWords.containsKey(str))
{
mapWords.put(str,1);
}
//如果map当前中包含该单词,那么取出该单词对应的值(即该单词已经出现的次数),将其加1后,保存回去。
else
{
Integer iCount = mapWords.get(str);
iCount++;
mapWords.put(str,iCount);
}
map统计单词次数的练习
最新推荐文章于 2021-12-26 15:35:21 发布