C#英汉翻译

要求:输入英文单词能够给出一个反馈;

准备工作:有一个英汉对应的txt文件;

代码实现:

<span style="font-size:18px;">namespace _07英汉翻译
{
    class Program
    {
        static void Main(string[] args)
        {
            //读文件
            string path = @"E:\C#程序\20121106解决方案\07英汉翻译\英汉词典TXT格式.txt";//自己存放的路径

            string[] text = File.ReadAllLines(path ,Encoding.Default );
            
            Dictionary<string, string> myDic = new Dictionary<string, string>();

            //遍历里面的英语和汉语,把英文作为key,中文作为value
            for (int i = 0; i < text.Length ; i++)
            {
                string[] strText = text[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                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 ();
        }
    }
}</span>
实现效果:

                    
       总结:Dictionary<TKey,Tvalue>用法:默认提供的命名空间System.Collections.Generic,
TKey:字典中的键的类型。TValue:字典中的值的类型,它们是一对存在的,用法类似于Hahtable.

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 37
    评论
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值