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

熱點內容
伺服器如何支持大量數據的讀寫 瀏覽:599
汽車壓縮機磁吸不轉 瀏覽:547
python做網站登錄 瀏覽:240
查看javaapi 瀏覽:952
編程貓電子表格 瀏覽:523
扣扣如何解除綁定的app 瀏覽:779
樂橙監控app如何用 瀏覽:196
編程最重要的是什麼 瀏覽:972
excel函數大全pdf 瀏覽:525
便宜雲伺服器服務 瀏覽:111
三星pdf軟體 瀏覽:290
子菜單里建立文件夾 瀏覽:777
用命令方塊驅除生物 瀏覽:723
老男孩python怎麼樣 瀏覽:86
為什麼python中etree報錯 瀏覽:958
普米康APP怎麼下載 瀏覽:877
抖音小店怎麼看加密訂單的信息 瀏覽:683
linux時間同步設置 瀏覽:462
數控車床實驗編程 瀏覽:109
百分三百的演算法 瀏覽:994