Document

其实这个就是为了显示一个代码的运行效果,统计一篇文档资料中单词的个数(提示文档资料可以放在字符串中)

这次先介绍运行效果,然后在写代码:

先输入句子:

输入自己要写的句子:chenyanlong  is  wo  is  wo!

现在就直接上代码了:

/****
*统计一篇文档资料中单词的个数(提示文档资料可以放在字符串中)。
* @author yanlong
* 2017/5/9
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;import
java.util.Set;
import javax.swing.JOptionPane;
public class test1 {
public static void main(String[] args) {
String s = JOptionPane.showInputDialog(null, "请输入句子------:");
String[] ss = s.trim().split(" ");
Map map = new HashMap();
for (int i = 0; i < ss.length; i++) {
int count = 0;
for (int j = 0; j < ss.length; j++) {
if (ss[i].equals(ss[j])) {
count = count + 1;
}
}
map.put(ss[i], count);
}
Set key=map.keySet();
for (Iterator iterator = key.iterator();
iterator.hasNext();
)
{
String name = (String) iterator.next();
String count2=map.get(name).toString();
System.out.println(name+"有 "+count2+" 个");
}
}
}

原文地址:https://www.cnblogs.com/chenyanlong/p/6832914.html