導航:首頁 > 編程語言 > aescjava

aescjava

發布時間:2022-10-02 05:12:19

java編程:輸入一段英文:1、統計一共有多少個單詞;2、每個單詞出現的次數;3、按出現次數升或降序排列

自己去調整,隨便寫的;

package com..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));
}
}
}

閱讀全文

與aescjava相關的資料

熱點內容
膨脹加強帶梁箍筋要不要加密 瀏覽:667
排序演算法大全及時間復雜度 瀏覽:341
壓縮文件時報錯 瀏覽:611
圖的演算法有什麼用 瀏覽:793
櫃式空調壓縮機在哪裡 瀏覽:724
創建多級文件夾視頻講解 瀏覽:195
計算機有很多空文件夾能刪嗎 瀏覽:486
android44w下載 瀏覽:248
測排卵app哪個好用 瀏覽:622
平時解壓方法圖解 瀏覽:56
php多長時間能學會 瀏覽:308
apex伺服器選什麼好 瀏覽:595
nds編譯 瀏覽:110
各個擊破法動態規劃法貪婪演算法 瀏覽:533
柱端部為什麼要加密 瀏覽:891
c單鏈表排序演算法 瀏覽:362
python鄰接鏈表 瀏覽:369
外國解壓的視頻說話助眠 瀏覽:115
查看php擴展版本 瀏覽:239
社保卡加密機放在信息中心 瀏覽:95