github:https://github.com/zhonghongyao/WordSplit.git
java
switch (count){ case 1:String strSource[]=line.split(" "); for (int i = 0; i < strArray.length; i++) { for (int j = 0; j < strSource.length; j++) { if(strArray[i].equals(strSource[j])){ arrCount[i]++; } } } default:String strAll[]=line.split(" "); for (int i = 0; i <strAll.length ; i++) { if(map.size()==0){ map.put(strAll[i],1); continue; } int size=map.size(); boolean flag=false; for (Map.Entry<String, Integer> entry : map.entrySet()) { if(entry.getKey().equals(strAll[i])){ int temp=entry.getValue(); entry.setValue(++temp); flag=true; } } if(flag==false){ map.put(strAll[i],1); } } break; } } //輸出 if(count==1){ for (int i = 0; i <strArray.length ; i++) { System.out.print(strArray[i]+"-----數量:"+arrCount[i]+" "); //柱形圖 } }else if(count==2){ Map<String,Integer>mapResult=MapSort.sortMapByValue(map); for (Map.Entry<String, Integer> entry : mapResult.entrySet()) { if(k==0){ break; }else { System.out.println("單詞= " + entry.getKey() + " 數量= " + entry.getValue()); k--; } } }else if(count==3){ Map<String,Integer>mapRe= MapSort.sortMapByValue(map); writeFile(mapRe); } } catch (IOException e) { e.printStackTrace(); } }
本項目採用類思想分塊處理,實現了軟件工程的「高內聚,低耦合」思想git