编写一个程序,实现具有如下功能的交互式字典:
(1)可以查询每个单词的解释。例输入"Hello"将显示"a greeting".
(2)能够加入新的单词和解释。
(3)能够删除单词和解释。
(4)将所有单词及解释保存在一个文件里,程序运行时可读取。
提示:编写一个word Dictionary类,提供字典对象,包含查询,增加,删除单词和解释,从文件读取字典和将字典保存到文件等方法。

解决方案 »

  1.   

    卖代码啦
    200多行呢,只给20分实在~_~!顾及到只是常用,所以使用的access匿名连接odbc方式。数据库文件名:dictionary.mdb,与class文件放在一起,或者修改程序27行为绝对路径。
    表名dictionary。
    第一列word,文本,主键,非空。
    第二列explain,文本,非空。
    长度越大越好。access好像只能到255。以下保存为MyDictionary.java。注意:我用了Scanner,所以只能在1.5.0下使用。
    实现列表、增、删、查功能。
    jdk1.5.0编译运行通过。//dictionary by 饿鱼骨头 05-11-13/***
     *use access to store the data
     ***/
     
    //import packages 
    import java.util.Scanner;
    import java.io.*;
    import java.sql.*;//class
    public class MyDictionary 
    {
    //variables
    private String cmd = "";
    private Connection conn;
    private Statement stmt;
    private PreparedStatement pStmt;
    private ResultSet rs;
    public MyDictionary()
    {
    try
    {
    //get connection to db
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:Driver=MicroSoft Access Driver (*.mdb);DBQ=dictionary.mdb");
    //reply the request
    while(!cmd.equals("Q"))
    {
    getCmd();
    if(cmd.equals("L"))
    {
    stmt = conn.createStatement();
    listWords();
    }
    if(cmd.equals("A"))
    {
    pStmt = conn.prepareStatement("insert into dictionary (word,explain) values(?,?)");
    addWord();
    }
    if(cmd.equals("U"))
    {
    pStmt = conn.prepareStatement("select [explain] from dictionary where [word] = ?");
    lookUp();
    }
    if(cmd.equals("D"))
    {
    pStmt = conn.prepareStatement("delete from dictionary where [word] = ?");
    delWord();
    }
    }
    }
    catch(Exception e)
    {
    System.out.println("Faile to connect to DB!");
    //e.printStackTrace();
    }
    //close link to the DB
    closeAll();
    }

    //method listWords
    public void listWords()
    {
    //variables
    String word;
    String explain;
    try
    {
    rs = stmt.executeQuery("select * from dictionary");
    if(rs.next())
    {
    word = rs.getString("word");
    explain = rs.getString("explain");
    System.out.println(word + " : " + explain);
    }
    }
    catch(Exception e)
    {
    System.out.println("List the words from dictionary denied!");
    //e.printStackTrace();
    }
    cmd = "";
    rs = null;
    word = null;
    explain = null;
    }

    //method addWord
    public void addWord()
    {
    //variables
    String word;
    String explain;
    //get messages from keyboard
    System.out.print("The word : ");
    //Scanner s = Scanner.create(System.in);
    Scanner s = new Scanner(System.in);
    word = s.nextLine();
    System.out.print("The explain : ");
    explain = s.nextLine();
    //add word-explain to dictionary.dic
    try
    {
    pStmt.setString(1,word);
    pStmt.setString(2,explain);
    pStmt.executeUpdate();
    System.out.println("[" + word + "] successfully be added to dictionary!");
    }
    catch(Exception e)
    {
    System.out.println("Add [" + word + "] to dictionary denied!");
    //e.printStackTrace();
    }
    cmd = "";
    s = null;
    word = null;
    explain = null;
    }

    //method lookUp
    public void lookUp()
    {
    //variables
    String word;
    String explain;
    //get message from keyboard
    System.out.print("The word you look up : ");
    Scanner s = new Scanner(System.in);
    word = s.nextLine();
    //get explain from dictionary
    try
    {
    pStmt.setString(1,word);
    rs = pStmt.executeQuery();
    if(rs.next())
    {
    explain = rs.getString("explain");
    System.out.println("explain is : " + explain);
    }
    else
    {
    System.out.println("No such word in the dictionary!");
    }
    }
    catch(Exception e)
    {
    System.out.println("Look up [" + word + "] from dictionary denied!");
    //e.printStackTrace();
    }
    cmd = "";
    s = null;
    rs = null;
    word = null;
    explain = null;
    }

    //method delWord
    public void delWord()
    {
    //variables
    String word;
    //get message from keyboard
    System.out.print("The word you want to delete : ");
    Scanner s = new Scanner(System.in);
    word = s.nextLine();
    //delete word from the dictionary
    try
    {
    pStmt.setString(1,word);
    pStmt.executeUpdate();
    System.out.println("[" + word + "] successfully be deleted from dictionary!");
    }
    catch(Exception e)
    {
    System.out.println("Delete [" + word + "] from dictionary denied!");
    //e.printStackTrace();
    }
    cmd = "";
    s = null;
    word = null;
    }

    //method getCmd
    private void getCmd()
    {
    Scanner s; 
    while(!cmd.equals("L")&&!cmd.equals("A")&&!cmd.equals("U")&&!cmd.equals("D")&&!cmd.equals("Q"))
    {
    //set cmd = null for GC
    cmd = null;
    //get message from keyboard
    s = new Scanner(System.in);
    System.out.println();
    System.out.print("(L)ist/(A)dd word/look (U)p/(D)elete word//(Q)uit?:");
    cmd = s.nextLine().toUpperCase(); 
    }
    s = null;
    }

    //method close
    private void closeAll()
    {
    try
    {
    conn.close();
    pStmt.close();
    }
    catch(Exception e)
    {
    System.out.println("Faile to close the link to DB!");
    //e.printStackTrace();
    }
    }

    //method main
    public static void main(String[] args)
    {
    MyDictionary d = new MyDictionary();
    }
    }
      

  2.   

    access就和word一样就一个文件嘛。
    或者用properties文件?
      

  3.   

    这个问题的源程序不止这个价格呀。。
    熟悉一下Java的IO操作,用文件实现很容易的
      

  4.   

    为什么一定要用文件啊,xls,dbf,mdb这些也都可以算是文件啊。