java代码题 给出一段英文句子,让你把句子里的每个单词(无缩写形式) 出现次数计算出来!

学习 时间:2026-04-01 12:25:57 阅读:9422
java代码题 给出一段英文句子,让你把句子里的每个单词(无缩写形式) 出现次数计算出来!我做的大概是 把句子定义String,之后先根据 正则判断 除空格外的一切非 字母的符号都不算,根据空格切分 ,用什么容器装 之后遍历循环,打印出来.希望懂的人帮忙写出来 泪奔.有点疑问 每一步 都是干什么的吗?

最佳回答

机灵的小笼包

高兴的钢笔

2026-04-01 12:25:57

等我,就写出来import java。io。*;import java。util。HashMap;import java。util。Map。Entry;public class Test {\x05\x05public static void main(String[] args) {\x05\x05// TODO Auto-generated method stub\x05\x05try {\x05\x05\x05BufferedReader br = new BufferedReader(new FileReader("C:\\text。txt"));//我就不写有界面的了,这个参数就是你英文句子文件所在位置\x05\x05\x05String sentence = null;\x05\x05\x05HashMap map = new HashMap();\x05\x05\x05while((sentence = br。readLine())!=null){\x05\x05\x05\x05sentence = sentence。replaceAll("[\\pP'$']","");\x05\x05\x05\x05String[] words = sentence。split(" ");\x05\x05\x05\x05for(String word:words){\x05\x05\x05\x05\x05if(map。get(word)==null)\x05\x05\x05\x05\x05map。put(word,1);\x05\x05\x05\x05\x05else map。put(word,map。get(word)+1);\x05\x05\x05\x05}\x05\x05\x05}\x05\x05\x05java。util。Iterator iter = map。entrySet()。iterator();\x05\x05\x05while(iter。hasNext()){\x05\x05\x05\x05Entry entry = (Entry) iter。next();\x05\x05\x05\x05System。out。println(entry。getKey()+" "+entry。getValue());\x05\x05\x05}\x05\x05} catch (FileNotFoundException e) {\x05\x05\x05// TODO Auto-generated catch block\x05\x05\x05e。printStackTrace();\x05\x05} catch (IOException e) {\x05\x05\x05// TODO Auto-generated catch block\x05\x05\x05e。printStackTrace();\x05\x05}}}

最新回答共有2条回答

  • 酷炫的黑米
    回复
    2026-04-01 12:25:57

    等我,就写出来import java。io。*;import java。util。HashMap;import java。util。Map。Entry;public class Test {\x05\x05public static void main(String[] args) {\x05\x05// TODO Auto-generated method stub\x05\x05try {\x05\x05\x05BufferedReader br = new BufferedReader(new FileReader("C:\\text。txt"));//我就不写有界面的了,这个参数就是你英文句子文件所在位置\x05\x05\x05String sentence = null;\x05\x05\x05HashMap map = new HashMap();\x05\x05\x05while((sentence = br。readLine())!=null){\x05\x05\x05\x05sentence = sentence。replaceAll("[\\pP'$']","");\x05\x05\x05\x05String[] words = sentence。split(" ");\x05\x05\x05\x05for(String word:words){\x05\x05\x05\x05\x05if(map。get(word)==null)\x05\x05\x05\x05\x05map。put(word,1);\x05\x05\x05\x05\x05else map。put(word,map。get(word)+1);\x05\x05\x05\x05}\x05\x05\x05}\x05\x05\x05java。util。Iterator iter = map。entrySet()。iterator();\x05\x05\x05while(iter。hasNext()){\x05\x05\x05\x05Entry entry = (Entry) iter。next();\x05\x05\x05\x05System。out。println(entry。getKey()+" "+entry。getValue());\x05\x05\x05}\x05\x05} catch (FileNotFoundException e) {\x05\x05\x05// TODO Auto-generated catch block\x05\x05\x05e。printStackTrace();\x05\x05} catch (IOException e) {\x05\x05\x05// TODO Auto-generated catch block\x05\x05\x05e。printStackTrace();\x05\x05}}}

上一篇 “快”,用英语怎么说?

下一篇 观察含羞草 400字作文