C#英汉翻译
2015-12-27 14:48
465 查看
要求:输入英文单词能够给出一个反馈;
准备工作:有一个英汉对应的txt文件;
代码实现:
总结:Dictionary<TKey,Tvalue>用法:默认提供的命名空间System.Collections.Generic,TKey:字典中的键的类型。TValue:字典中的值的类型,它们是一对存在的,用法类似于Hahtable.
准备工作:有一个英汉对应的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.
相关文章推荐
- 用C#实现通用的DAL Insert
- c#中调用DLL
- Protobuf C#教程 ThriftC#教程大合辑
- C#之try-catch
- 【C#】 多态
- C#_List<T>的使用
- C# 中如何将一个类文件(XX.CS)封装成.dll文件
- c# 委托和事件(总结篇)
- 初识C#
- C#大框架小要素
- C#制作、打包、签名、发布Activex全过程(转)
- C#如何以管理员身份运行程序(转)
- CLR via C#深解笔记三 - 基元类型、引用类型和值类型 | 类型和成员基础 | 常量和字段
- C#委托&Lambda
- C#操作Access类
- C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串
- 获取C#代码执行的时间(精确到毫秒)
- C#中timer类 在C#里关于定时器类就有3个
- 如何在 C#中添加 dll 文件
- 【整理】C#文件操作大全(SamWang)