栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【JAVA程序设计基础】字典程序(内存版)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【JAVA程序设计基础】字典程序(内存版)

package dict;

import java.io.*;
import java.util.*;

public class Dict2 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        System.out.println("###################################");
        System.out.println("###################################");
        System.out.println("#########欢迎进入英文字典程序#########");
        System.out.println("###################################");
        System.out.println("###################################");

        //1.读取文件
        InputStream is = Dict1.class.getResourceAsStream("words.txt"); //获取文件路径
        Scanner isSc = new Scanner(is); //转化为文件流

        Map dictMap =  new HashMap(); //创建字典用于存储文件中的数据

        //2.将文件内容存入Map中
        while (isSc.hasNext()) { //死循环
            String[] strings=isSc.nextLine().split("t"); //按行读取文本内容,用t分隔字符串
            if (strings.length>1) { //因为文本中存在只有单词无翻译的情况,所以做此判断
                dictMap.put(strings[0],strings[1]); //将字符串数组中的数据存入字典中
            }else {
                dictMap.put(strings[0],null);
            }
        }

        //3.输入要查找的单词,返回结果
        while(true){
            System.out.print("请输入你要翻译的英文单词(回车退出):");
            String input = sc.nextLine(); //nextLine()吸取字符前后的空格/Tab键,回车键截止

            if(input.equals("")){
                break;
            }else{
                if(dictMap.containsKey(input)){ //判断字典中是否存在键
                    System.out.println(input+"t"+dictMap.get(input)); //通过键获取值
                }else{
                    System.out.println("非常遗憾,没有找到你要翻译的单词 "+input);
                }
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763117.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号