码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-02-24 09:39:26      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:


要求 输入英文翻译成中文

输入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 欢迎

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

标签:

原文地址:http://blog.csdn.net/yayun0516/article/details/50722622

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!