T001_UT001_0017

本文介绍了一个Java程序,该程序能够接收用户输入的英文句子,并统计除去标点符号外所有单词的出现频率。统计结果会按照Java中字符串的自然顺序进行排序输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写一个程序,从标准输入设备上输入一行英文语句。敲击回车后对该语句进行分析,统计除逗号,句号,感叹号和问号以外的所有单词的频次并打印在标准输出设备上。输出顺序按Java语言的字符串的自然顺序排序。

字符串自然顺序是指,对字符串从左到右,每个字符按照其字符的ascii码从小到大排序,如果第1个字符相同,则比较第二个字符,然后以此类推。

举例一:

输入:

1
This is a very simple problem. I can solve this in a minute! I don't think this is a simple problem....
输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
I,2
This,1
a,3
can,1
don't,1
in,1
is,2
minute,1
problem,2
simple,2
solve,1
think,1
this,2
very,1

import java.util.*;
public class T001_UT001_0017
{
	public static void main(String[] args)
	{
		Scanner njp_input=new Scanner(System.in);
		String njp_str=njp_input.nextLine();
		StringBuffer njp_StringBuffer = new StringBuffer();
		TreeMap<String ,Integer> njp_TreeMap = new TreeMap<String ,Integer> ();  
		String[] njp_str_list = njp_str.split(" |,|\\?|\\.|\\!|\\....");
		for (int i = 0; i < njp_str_list.length; i++) 
	   	{
	    	if (!njp_TreeMap.containsKey(njp_str_list[i])) 
	        	njp_TreeMap.put(njp_str_list[i], 1);
			else 
	        	njp_TreeMap.put(njp_str_list[i],njp_TreeMap.get(njp_str_list[i])+1 );
	   	}
		Iterator<String> njp_iterator = njp_TreeMap.keySet().iterator();
		int count=0;
		while(njp_iterator.hasNext())
	    {
	        String word = (String)njp_iterator.next();
	        if(count!=0)
	        	njp_StringBuffer.append(word).append(",").append(njp_TreeMap.get(word)).append("\n");
	        count++;
        }
		System.out.print(njp_StringBuffer.toString());
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gabanon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值