上机题目(高级)- 电子词典(Java)

本文介绍了一个简单的英汉词典查询程序实现方法。该程序使用Java编写,能够从本地文件加载词典数据,并支持用户输入英文单词查询对应的中文翻译。此外,还提供了帮助功能,列出所有可查询的单词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


要求 输入英文翻译成中文

输入help输出所有单词

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class ReadDic {
 public static String readDicFile(String filePath) {
  String result = "";
  try {
   String encoding = "GBK";
   File file = new File(filePath);
   if (file.isFile() && file.exists()) { //判断文件是否存在 
    InputStreamReader read = new InputStreamReader(
      new FileInputStream(file), encoding);//考虑到编码格式 
    BufferedReader bufferedReader = new BufferedReader(read);
    String dicFullText = null;
    while ((dicFullText = bufferedReader.readLine()) != null) {
     result = dicFullText;
    }
    read.close();
   } else {
    System.out.println("找不到指定的文件");
   }
  } catch (Exception e) {
   System.out.println("读取文件内容出错");
   e.printStackTrace();
  }
  return result;
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  Scanner cin = new Scanner(System.in);
  String input = cin.next();
  String dicPath = "D:/zhangyayun 13057655618/Dic/doc.txt";
  String result = readDicFile(dicPath);
  String dicText[] = result.split("\\|");
  TreeMap tm = new TreeMap();
  for (int i = 0; i < dicText.length; i++) {
   String temp = dicText[i];
   String tempArray[] = temp.split("=");
   tm.put(tempArray[0], tempArray[1]);
  }
  if (input.equals("help")) {
   Iterator it = tm.entrySet().iterator();
   while (it.hasNext()) {
    Map.Entry entry = (Map.Entry) it.next();
    Object key = entry.getKey();
    Object value = entry.getValue();
    System.out.println(key + " " + value);
   }
  } else {
   String dicResult = (String) tm.get(input);
   if (dicResult != null) {
    System.out.println(tm.get(input));
   } else {
    System.out.println("input error");
   }
  }
 }
}
 


help
hello 你好
man 男人
welcome 欢迎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值