導航:首頁 > 編程語言 > 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相關的資料

熱點內容
如來佛祖命令雷神去下界 瀏覽:854
新電腦管家下載好怎麼解壓 瀏覽:528
php獲取介面數據 瀏覽:763
最後的命令 瀏覽:921
如何添加手機app桌面快捷圖標 瀏覽:427
ui設計師與程序員 瀏覽:417
壽司pdf 瀏覽:828
pythonbg是什麼 瀏覽:248
c數值演算法程序大全 瀏覽:785
android整點報時 瀏覽:221
稀土pdf 瀏覽:536
單片機電子鎖 瀏覽:596
通達信機智資金流指標公式源碼 瀏覽:216
php安裝xsl擴展 瀏覽:842
python如何使用help 瀏覽:367
上汽榮威app在哪裡查詢 瀏覽:903
冰櫃壓縮機溫度108 瀏覽:720
阿里雲郵smtp伺服器地址 瀏覽:252
解壓館認知理解 瀏覽:239
為什麼使用非官方伺服器會封號 瀏覽:9