A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiaqingchao 中级黑马   /  2013-2-18 13:00  /  933 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

using System.IO;
namespace _英汉翻译
{
     class Program
     {
         static void Main(string[] args)
         {
             //读文件
             string path = @"I:\1018基础班课程资料\20121106C#基础\资料\英汉词典TXT格式.txt";
             string []text=File.ReadAllLines(path,Encoding.Default);
             Dictionary<string, string> myDic = new Dictionary<string, string>();
             //遍历里面的英语和汉语,想办法 把 英文作为key,z中文作为value
            for (int i = 0; i < text.Length; i++)
             {
                 //定义一个数组用来存切割后的英文和中文
                 string[] strText=text[i].Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);//切割每一行的空格
                 //英文赋值给english
                 string english = strText[0];
                 string chinese = "";//定义一个变量用来存中文意思
                 for (int j = 1; j < strText.Length; j++)
                 {
                     chinese += strText[j];//把这个单词后面所有的中文加到一起
                 }
                 if (!myDic.ContainsKey(strText[0]))
                 {
                     myDic.Add(english, chinese);
                 }
                 else
                 {
                     //把相同单词的中文意思放到一起
                     myDic[english] += chinese;
                 }
             }
             Console.WriteLine("请输入英文单词");
             string str = Console.ReadLine();
             if (myDic.ContainsKey(str))
             {
                 Console.WriteLine(myDic[str]);
             }
             else
             {
                 Console.WriteLine("字典中没有这个单词");
             }

            Console.ReadKey();
         }
     }
}

评分

参与人数 1技术分 +1 收起 理由
张文 + 1

查看全部评分

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马