Java编程:输入一段英文:1、统计一共有多少个单词;2、每个单词出现的次数;3、按出现次数升或降序排列 15
求大神帮忙呀,,很急,,,如果不想公开的话,就发我邮箱吧956389598@qq.com感谢ing………………...
求大神帮忙呀,,很急,,,如果不想公开的话,就发我邮箱吧956389598@qq.com
感谢ing……………… 展开
感谢ing……………… 展开
2个回答
展开全部
自己去调整,随便写的;
package com.baidu.com.java;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MatchWorld {
public static void main(String[] args) {
String str = "Is there anyone who hasn't suffered for the secret love? We alwaysthink that"
+ " love is very heavy, heavy and could be the heaviest thing inthe world. "
+ "But one day, when you look back, you suddenly realize thatit's always light, "
+ "light. We all thought love was very deep, but infact it's very thin. " + "The deepest and heaviest love must grow up withthe time";
// 全部转换成大写
str = str.toUpperCase();
//或许你可以考虑用空格分割
String[] rang = str.split("\\b");
// 自己去调如瞎整吧,如果要得到精确的
System.out.println("单词总数大概为:" + rang.length);
// 用来记录的单词
Map<String, Integer> map = new HashMap<String, Integer>();
int countSpace = 0;
//统计各个字宏橡正符出现的次数
for (String s : rang) {
if (s.trim().length() > 0) {
s = s.trim();
if (!map.containsKey(s)) {
int count = str.split("\\b" + s.trim() + "\\b").length;
map.put(s, count);
}
} else {
map.put("空格", countSpace++);
}
}
//单词出现次数
for (String key : map.keySet()) {
System.out.println(key + "出现:" + map.get(key) + "次");
}
//把元素添加到list
List<String> list = new ArrayList<String>();
list.addAll(map.keySet());
// 排序前
System.out.println("\n排序前:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
// 开始排序
System.out.println("\n按照出现次数降序排列(AESC):\n");
for (int i = 1; i < list.size(); i++) {
for (int j = 0; j < list.size() - i; j++) {
if (map.get(list.get(j)) > map.get(list.get(j+1))) {
String tmp = list.get(j);
list.set(j, list.get(j+1));
list.set(j + 1, tmp);
}
}
}
/蔽悔/ 排序后
System.out.println("\n排序后:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
}
}
package com.baidu.com.java;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MatchWorld {
public static void main(String[] args) {
String str = "Is there anyone who hasn't suffered for the secret love? We alwaysthink that"
+ " love is very heavy, heavy and could be the heaviest thing inthe world. "
+ "But one day, when you look back, you suddenly realize thatit's always light, "
+ "light. We all thought love was very deep, but infact it's very thin. " + "The deepest and heaviest love must grow up withthe time";
// 全部转换成大写
str = str.toUpperCase();
//或许你可以考虑用空格分割
String[] rang = str.split("\\b");
// 自己去调如瞎整吧,如果要得到精确的
System.out.println("单词总数大概为:" + rang.length);
// 用来记录的单词
Map<String, Integer> map = new HashMap<String, Integer>();
int countSpace = 0;
//统计各个字宏橡正符出现的次数
for (String s : rang) {
if (s.trim().length() > 0) {
s = s.trim();
if (!map.containsKey(s)) {
int count = str.split("\\b" + s.trim() + "\\b").length;
map.put(s, count);
}
} else {
map.put("空格", countSpace++);
}
}
//单词出现次数
for (String key : map.keySet()) {
System.out.println(key + "出现:" + map.get(key) + "次");
}
//把元素添加到list
List<String> list = new ArrayList<String>();
list.addAll(map.keySet());
// 排序前
System.out.println("\n排序前:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
// 开始排序
System.out.println("\n按照出现次数降序排列(AESC):\n");
for (int i = 1; i < list.size(); i++) {
for (int j = 0; j < list.size() - i; j++) {
if (map.get(list.get(j)) > map.get(list.get(j+1))) {
String tmp = list.get(j);
list.set(j, list.get(j+1));
list.set(j + 1, tmp);
}
}
}
/蔽悔/ 排序后
System.out.println("\n排序后:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询