java 以a為開頭單詞的詞典查詢樣本

來源:互聯網
上載者:User

標籤:文字檔   運行   can   對象   ati   思路   因此   main   儲存   

java中HashMap類表示為字典類,其中key,value一一對應的原則。因此是詞典查詢的首要工具。(HashMap字典類字面意思也可以看出~~)

程式思路:

程式開始前,應先建立一個字典文本用於單詞詞庫的儲存。

先讀取文字檔,因為每行為一個單詞和其釋義,所以採用逐行讀取的方法。

將每行的單詞和釋義分隔開,將前後兩部分分別存入HashMap對象當中。

最後輸入查詢單詞,在key中尋找關鍵詞即可擷取釋義。

代碼如下:

import java.io.*;import java.util.*;public class Hash_map {    public static void main(String[] args) {        Scanner in=new Scanner(System.in);        File fi=new File("dict.txt");        try {            BufferedReader dict=new BufferedReader(new InputStreamReader(new FileInputStream(fi)));            String line_text="";            String unknow;            HashMap find_word=new HashMap();            int i=0;            while(i<562){                line_text=dict.readLine();                //System.out.println(line_text);                String[] _map=line_text.split(" ");                //System.out.println(_map[0]+_map[_map.length-1]);                find_word.put(_map[0], _map[_map.length-1]);                                i+=1;            }            System.out.println("查詢首字母為a的單詞!當輸入為q時推出程式!");            System.out.println("輸入要尋找的單詞:");            unknow=in.next();            while(unknow.equals("q")==false){                System.out.println(find_word.get(unknow.toLowerCase()));                System.out.println("輸入要尋找的單詞:");                unknow=in.next();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("查詢結束!");        word_up_find();    }    public static void word_up_find(){        Scanner in=new Scanner(System.in);        int count=0;        System.out.println("判斷字串中大寫字母的個數!以輸入q結束");        String word=in.next();        while(word.equals("q")==false){            char[] st=word.toCharArray();            for(int i=0;i<st.length;i++){                if(st[i]>=‘A‘&&st[i]<=‘Z‘)                    count+=1;        }            System.out.println(count);            System.out.println("輸入要判斷的單詞:");            word=in.next();        }        System.out.println("判斷結束!");                    }}

在dos視窗中運行,

 

java 以a為開頭單詞的詞典查詢樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.