C#面向对象Chatbot智能版

大家好,我是一个C#的初学者,老师平时叫我们写C#控制台程序,我总结了一些所学的知识,写了一个升级版的聊天机器人,通过文件操着,机器人具有智能查找,学习功能。和大家分享一下,希望能得到高手指点进步。

1、Project:chatbot 2、功能:聊天、报时间 3、预置聊天学习,智能学习,做数学题 5、关键技术:读取文件,写入文件 6、打印聊天内容

二、要求

1、成员分工:主界面、预定义库、计算器 2、技术核心内容:文件流、write,read,array; 3、优化过程

<!-- lang: c# -->

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;

namespace ProjectC_sharp { class chatbot_61 { public static void Main(string[] args) { methot met = new methot(); //实例化方法过程 met.showUI(); //UI string chat; bool flag = false; //判断 string user; //定义用户名 string Robot; //定义机器人名字

    start:{Console.WriteLine("**********************************************************");Console.WriteLine("请选择机器人>>>>>>>[A :\"小屌丝\"]\t[B:\"小橘子\"]<<<<<<< ");  //start set chat botConsole.WriteLine("*        help>>>help                                                *");Console.WriteLine("**********************************************************");}Console.WriteLine("请输入您的姓名:");user = Console.ReadLine();Console.WriteLine("请选择机器人:");string set = Console.ReadLine();  //选择机器人if (set == "A" || set == "小屌丝" || set == "a"){met.ASayhello(user);Robot = "小屌丝";}else if (set == "B" || set == "小橘子" || set == "b"){met.BSayhello(user);Robot = "小橘子";}else{Console.Clear();Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("--------!waring:你的输入不正确,请重新输入----------\a\a\a\n");met.help(); //调出帮助菜单Console.WriteLine();Console.ForegroundColor = ConsoleColor.White;  //白色goto start;  //转到开始部分}methot.mcname = Robot;  //静态传值机器人名字methot.user = user; //静态传用户姓名do //机器人循环说话Main{Console.Write(user+">>说:");chat = Console.ReadLine();Console.WriteLine();met.Speak(chat,flag);if (chat == "exit" || chat == "EXIT")break;} while (true); //结束程序Console.Clear();Console.WriteLine();Console.WriteLine();Console.WriteLine();Console.WriteLine("\t\t>>>>系统已成功退出,按任意键结束程序!<<<<");Console.ReadKey();}
}class propert
{//机器人属性: 年龄、性别、姓名private string name;private int age;private string sex;public string Name{set { this.name = value; }get { return name; }}public int Age{set { this.age = value; }get { return age; }}public string Sex{set { this.sex = value; }get { return sex; }}public propert(string N, int A, string S){name = N;age = A;sex = S;}}class methot
{//机器人的名字public static string mcname;//用户的名字public static string user;/*以下函数是机器人的方法*///小屌丝Sayhellopublic string ASayhello(string user){propert pr = new propert("小屌丝", 18, "男");Console.WriteLine("{0}:你好,我叫<{1}>,我{2}岁了,我是{3}生\a", pr.Name = "小屌丝", pr.Name = "小屌丝", pr.Age, pr.Sex);//Console.WriteLine("请问你叫什么名字呀?\a");//string user = Console.ReadLine();Console.WriteLine("{0}:{1}很高兴和你聊天!\a", pr.Name, user);return user;}//小橘子Sayhellopublic void BSayhello(string user){propert pg = new propert("小橘子", 16, "女");Console.WriteLine("{0}:你好我叫<{1}>,我{2}岁了,我是{3}生\a", pg.Name = "小橘子", pg.Name = "小橘子", pg.Age, pg.Sex);Console.WriteLine("{0}:{1}很高兴和你聊天!\a", pg.Name, user);}//UIpublic void showUI(){Console.ForegroundColor = ConsoleColor.Green;Console.WriteLine("\t\t*****************************************");Console.WriteLine("\t\t*                                       *");Console.WriteLine("\t\t--------------wecomel chat bot---------- ");Console.WriteLine("\t\t*              聊天机器人               *");Console.WriteLine("\t\t*                                       *");Console.WriteLine("\t\t*                 >> 项目五组:农夫三拳  *");Console.WriteLine("\t\t*****************************************");Console.ReadKey();Console.ForegroundColor = ConsoleColor.White;Console.Clear();}/*help帮助中心*/public void help(){Console.WriteLine("**********************************************");Console.WriteLine("*                                            *");Console.WriteLine("*            CHAT BOT HELP CENTER            *");Console.WriteLine("*  1、select chat bot press \"A\" or \"B\"   *");Console.WriteLine("*  2、chat bot include weather、date、time   *");Console.WriteLine("*  3、Exit input \"exit\" or \"!e\"          *");Console.WriteLine("*  4、Calculator function, enter a formula   *");Console.WriteLine("*  5、the first number second number         *");Console.WriteLine("**********************************************");Console.ReadKey();Console.Clear();}/*查询时间*/public void datetime(){DateTime current = DateTime.Now;int year = current.Year;  //获取年int month = current.Month;  //获取月int day = current.Day;//获取日int hours = current.Hour;//获取小时int minute = current.Minute;// 获取分钟int second = current.Second;//获取秒if (hours <=6){Console.WriteLine("现在是凌晨。\a");}else if (hours <= 9){Console.WriteLine("早上好!\a");}else if (hours <=18){Console.WriteLine("下午好!\a");}else if(hours<=24){Console.WriteLine("晚上好!");}else{Console.WriteLine("__获得时间出错,请调试!");}Console.WriteLine("现在是{0}年{1}月{2}日{3}时{4}分{5}秒\a", year, month, day, hours, minute, second);}/*计算器模块*/public void Calculator(){double Number1, Number2;char option;double Result;bool flag = true;while(flag){try{Console.WriteLine("Enter the Fist Number");Number1 = double.Parse(Console.ReadLine());Console.WriteLine("Enter the Second Number");Number2 = double.Parse(Console.ReadLine());Console.WriteLine("Mian Menum");Console.WriteLine("1.Additon");Console.WriteLine("2.Subtraction");Console.WriteLine("3.Multiplication");Console.WriteLine("4.Division");Console.WriteLine("Enter the Operation you want to perform");option = char.Parse(Console.ReadLine());switch (option){case '1':Result = Number1 + Number2;Console.WriteLine("The result of addition is :{0}", Result); break;case '2':Result = Number1 - Number2;Console.WriteLine("The result of subtraction is :{0}", Result); break;case '3':Result = Number1 * Number2;Console.WriteLine("The result of Multiplication if :{0}", Result); break;case '4':if (Number2 == 0){Console.WriteLine("Divisor canot be zero");}else{Result = Number1 / Number2;Console.WriteLine("The result of divison is :{0}", Result);}break;default: Console.WriteLine("Invalid Option"); break;}flag = false;}catch{flag = true;Console.WriteLine("你的输入正确,请重新输入!\a\a\a");}}}/*test temp*/public void FileRead(){string FILENAME = "J:\\C_sharp_Code\\Project\\Answer.txt";FileStream fs = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);StreamReader sr = new StreamReader(fs, Encoding.Default);string reder = sr.ReadLine();while (reder != null){Console.WriteLine(reder);reder = sr.ReadLine();}sr.Close();fs.Close();}//机器人预定义内置库回复public void Speak(string chat,bool flag){//string weather="天气";//string time="时间";//string Calculate = "计算";//string[] str = { "天气","长沙","今天" };bool b;int count = 0;string FILENAME1 = "J:\\C_sharp_Code\\Project\\Question.txt";FileStream fs1 = new FileStream(FILENAME1, FileMode.Open, FileAccess.Read);StreamReader sr1 = new StreamReader(fs1, Encoding.Default);string reader1;reader1 = sr1.ReadLine();while (reader1 != null){count++;if (b = reader1.Contains(chat) /*|| chat.Contains(reader1)*/)//reader1.Contains(chat){// chat = reader1;  //rederflag = true;break;}reader1 = sr1.ReadLine();}// Console.WriteLine("判断:{0}", flag); Test 判断是否为真fs1.Close();sr1.Close();/*小调试*///  Console.WriteLine("count1={0}",count);  //测试count1// Console.WriteLine("chat={0}",chat);  //测试chat//  Console.WriteLine(flag);               //测试真假/*预定义内置库 weather 、计算器、简单回复*/if (chat.Contains("weather") || chat.Contains("天气") || chat.Contains("长沙"))             //关键字提取  天气{Console.WriteLine("{0}说>>:长沙的天气多云转晴\a",mcname);}//b = chat.Contains(Calculate)else if (chat.Contains("时间") || chat.Contains("几点") || chat.Contains("钟")||chat.Contains("time"))             //关键字提取   时间{// Console.WriteLine("现在的时间我还真不知道!");Console.Write("{0}说>>:",mcname);datetime();                                       //调用时间函数}else if (chat.Contains("计算") || chat.Contains("数学")||chat.Contains("做题")){Console.ForegroundColor = ConsoleColor.Yellow; //黄色Console.WriteLine("{0}说>>:好的,正在为你启动计算器",mcname);Calculator();Console.WriteLine();Console.WriteLine();Console.ForegroundColor = ConsoleColor.White;  //白色}else if (chat.Contains("....") || chat.Contains("...") || chat.Contains("..") || chat.Contains(".")){Console.WriteLine("{0}说>>:这斑斑点点,不能彰显你的沉默,只会昭示你左右手的寂寞~",mcname);}else if(chat.Trim().Equals(string.Empty))  //判断用户输入的是否空格{Console.WriteLine("{0}说>>:您什么也没有输入\a",mcname);}else if (chat==""){Console.WriteLine("{0}说>>:你一句话也没有说啊,你在吐口水吗?\a",mcname);}else if (flag){ReaderQuestion(chat,mcname); //question}else{//bool yn = true;Console.WriteLine("{0}说>>:~噢我没有听懂,你教我好吗?",mcname);while (yn){Console.ForegroundColor = ConsoleColor.DarkCyan;Console.WriteLine("{0}说>>:教我学习吧(Y\\N)?\a",mcname);Console.Write("{0}>>说:",user);string repor = Console.ReadLine();if (repor == "y" || repor == "Y"){Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine();Console.WriteLine();Console.WriteLine();Console.WriteLine("\t\t>>>>>>>>>机器人已进入学习模式<<<<<<<<<<<<");Console.WriteLine();Console.ForegroundColor = ConsoleColor.DarkCyan;WriteQuestion();  //调用学习方法yn = false;}else if (repor == "n" || repor == "N"){Console.WriteLine("{0}说>>:您取消了学习,哼~居然不教我。\a",mcname);yn = false;}else{Console.WriteLine("{0}说>>:您的输入不正确\a",mcname);yn = true;}Console.WriteLine();Console.ForegroundColor = ConsoleColor.White;  //白色}}}/*机器人学习*//*test Reader*/public void ReadAnser() //读取Answer—A文件{string FILENAME = "J:\\C_sharp_Code\\Project\\Answer.txt";FileStream fs = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);StreamReader sr = new StreamReader(fs, Encoding.Default);string reder = sr.ReadLine();while (reder != null){Console.WriteLine(reder);reder = sr.ReadLine();}sr.Close();fs.Close();}/*机器人智能匹配模块*/public void ReaderQuestion(string chat, string name ) //读取Questioin第二文件B{bool b;  //flagint count = 0;string FILENAME1 = "J:\\C_sharp_Code\\Project\\Question.txt";string FILENAME2 = "J:\\C_sharp_Code\\Project\\Answer.txt";FileStream fs1 = new FileStream(FILENAME1, FileMode.Open, FileAccess.Read);FileStream fs2 = new FileStream(FILENAME2, FileMode.Open, FileAccess.Read);StreamReader sr1 = new StreamReader(fs1, Encoding.Default);StreamReader sr2 = new StreamReader(fs2, Encoding.Default);string reader1;reader1 = sr1.ReadLine();while (reader1 != null){count++;if (b = reader1.Contains(chat)/*||chat.Contains(reader1)*/)    //1、核心关键捕获,智能匹配//{chat = reader1;  //reder//Console.WriteLine(chat);break;         //找到坐标位置跳出去}reader1 = sr1.ReadLine();// Console.WriteLine(chat);}//指针指向Anser//Console.WriteLine("count={0}",count); //坐标位置测试string reder2 = "null没有读到";for (int i = 1; i <= count; i++){reder2 = sr2.ReadLine();chat = reder2;}Console.ForegroundColor = ConsoleColor.Green;Console.WriteLine("{0}说>>{1}\a", name, chat);  //机器人的回答Console.WriteLine();sr1.Close();fs1.Close();sr2.Close();fs2.Close();}/*Write机器人学习过程*/public void WriteAnwser()  //WriterAnswer_  B文件{string FILENAME = @"J:\C_sharp_Code\Project\Answer.txt";FileStream feel = new FileStream(FILENAME, FileMode.Append, FileAccess.Write);StreamWriter sw = new StreamWriter(feel, Encoding.Default);Console.WriteLine("请输入答案:");//写入文件string str = Console.ReadLine();sw.WriteLine(str + "\n");sw.Flush();sw.Close();feel.Close();Console.WriteLine();Console.ForegroundColor = ConsoleColor.Blue;Console.WriteLine("{0}说>>:OK,我学会了!",mcname);Console.WriteLine("~亲咱们继续聊!");Console.WriteLine();Console.WriteLine();Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("\t\t>>>>>>>>>机器人学习已成功<<<<<<<<<<<<");Console.WriteLine();Console.ForegroundColor = ConsoleColor.White;  //白色}public void WriteQuestion()  //Write Question  A文件{string FILENAME = @"J:\C_sharp_Code\Project\Question.txt";FileStream feel = new FileStream(FILENAME, FileMode.Append, FileAccess.Write);StreamWriter sw = new StreamWriter(feel, Encoding.Default);//写入文件Console.ForegroundColor = ConsoleColor.Green;  //绿色Console.WriteLine("请输入问题:");string str = Console.ReadLine();sw.WriteLine(str + "\n");sw.Flush();sw.Close();feel.Close();WriteAnwser(); //调用回答方法}}

}

转载于:https://my.oschina.net/u/1156135/blog/137856

C#面向对象Chatbot智能版相关推荐

  1. 网页版电脑桌面远程操控_我真的再也不买电暖器了 - 电暖器智能版

    大家好我是被电暖器缠绕的呱呱蛙,上次连续购入两个电暖器之后感觉生活非常美好.然后俺娘又给亲戚开始安利了起来,结果在双11那天又接到一个任务,买一个既能烘衣服又能手机控制的电暖器,顺便到货了之后让我帮忙 ...

  2. matlab面向对象多态性,MATLAB面向对象程序设计10版.pptx

    MATLAB面向对象程序设计10版.pptx MATLAB面向对象程序设计;第1章 绪言;1.1如何学会一门编程语言;1.2做笔记的方法;第2章 MATLAB基础知识;MATLAB的help文档;Ma ...

  3. 金簿财务软件智能版3.985

    金簿财务软件智能版3.985 金簿财务软件是一款运行于windows平台的财务软件,具有简单.实用.易学的特点,是财务人员的真正帮手,适用于广大中小企业.行政事业单位! 它具有以下特点:     1. ...

  4. 闪电邮新建html模板,非常靠谱之网易智能版闪电邮6大新功能评测

    最近,湖南卫视节目<非常靠谱>正在热播中,小编今天就为节目组推荐一款"非常靠谱"的邮件客户端软件.经小编试用,这款被称为智能版闪电邮的2.0.2正式版新配备了写信模板. ...

  5. Q宠猪猪伴侣 V2.43 - 智能版~~ 天空原创软件

    软件名字:Q宠猪猪伴侣 V2.43 智能版 软件介绍: Q宠猪猪伴侣,是国内领先推出的一款Q宠猪猪辅助软件,她能代替人工操作,自动照顾您的Q宠猪猪,她支持自动喂养.自动洗澡.自动打工.自动学习.它还会 ...

  6. 手电筒安卓_4G手机+AI智能音响+收音机+手电筒+唱戏机+插卡音箱=HT318智能版

    4G手机+AI智能音响+收音机+手电筒+唱戏机+插卡音箱=HT318AI智能版 红桃收音机三代升级了! 升级全网4G,移动联通电信都支持! 升级AI搜歌,语音搜歌,老年人的智能福利! 内置喜马拉雅内容 ...

  7. java面向对象电子科大版答案_电子科大17秋《面向对象程序设计》在线作业1

    电子科大17秋<面向对象程序设计>在线作业1 ----------------------------------------------------------------------- ...

  8. JAVA面向对象程序设计(第二版) 袁绍欣 第四章答案

    面向对象(上) 1.名词解释:构造方法.抽象 构造方法一般是用来初始化数据成员的,与类名相同,无返回值. 分为有参和无参数,当一个类中没有定义构造函数时,系统会给该类中加一个默认的空参数的构造函数,方 ...

  9. 【面向对象】大白话版(一)------【海贼王】

    前言:每当提到面向对象的时候,初学者肯定都是一脸懵逼的状态,到底什么是面向对象?会用面向对象后有什么牛逼之处吗?不会用是不是就会死掉?答案肯定不会死掉,我们可以来简单的举一 个栗子 1.当你想到熊猫的 ...

最新文章

  1. mvn 默认scope_maven scope 的作用
  2. 有向图的拓扑排序的理解和简单实现(Java)
  3. kafka消息存储原理及查询机制
  4. CRMEB系统安装访问不了
  5. Spark Streaming在Spark Web UI上的显示办法
  6. python3之MongoDB
  7. 您可能不知道的 C++ 关键字
  8. c++ try catch语句_再问你一遍,你真的了解try..catch(finally)吗???
  9. svn locked解决方法
  10. Vmware安装Centos7上网问题的解决
  11. 李迟2021年6月知识总结
  12. excel处理后上传数据库
  13. XML DOM Object Model in .NET [3/3] - Samples
  14. 程序员面试金典——17.6最小调整有序
  15. python将嵌套列表转换为字典
  16. 恩施软件开发人员每月多少钱_软件开发人员外包报价表
  17. 亚马逊 站内广告数据分析
  18. Halcon仿射变换图片(旋转、缩放、平移)
  19. win10开机就卡死在桌面上怎么解决
  20. 计算机应用开机禁止打开,怎样设置电脑开机启动项禁止

热门文章

  1. 那些年遇到的DB2大坑
  2. Citus集群拓扑架构介绍
  3. 使用scrapy +selenium爬取动态渲染的页面
  4. 在win10系统中安装Visual C++ 6.0的具体方法
  5. 美国军方又出黑科技,实现用意念控制无人机
  6. H264编码器5( x264源代码简单分析:x264_slice_write() 与H264 编码简介)
  7. 2020最新版前端学习路线图
  8. Java多线程编程(四)——死锁问题
  9. 使用Cocoapods创建私有podspec
  10. 2022丘成桐数学竞赛放榜!清华3金10银霸榜