利用类及其属性方法来写个简单的聊天机器人

来源:互联网 发布:武汉淘宝商学院靠谱吗 编辑:程序博客网 时间:2024/05/21 07:45

程序要求机器人设一个饥饿度,且可以吃东西。

要有多个机器人可供选择。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            机器人 p = new 机器人();                          p.Eat(5);            string n = Name(ref p);            p.Say("姓名");                        while (true)             {                                            n= Console.ReadLine ();              if (n == "Exit")              {                  Console.WriteLine("你已经退出聊天程序,按任意键退出程序");                  break;              }                      else              {                  p.Say(n);              }            }            Console.ReadKey();                    }        static string  Name(ref 机器人 p)        {            Console.Write("请选择机器人\"Tom\"或\"Kity\",输入T或K: ");            string n = Console.ReadLine();            switch (n)            {                case "t":                case "T":                    p.Name = "Tom";                    break;                case "k":                case "K":                    p.Name = "Kity";                    break;                default:                    Name(ref p);                    break;            }            return n;        }    }    class 机器人    {        public string Name { get; set; }        private int FullLevel { get; set; }        public void PrintSay(string s1)        {            Console.WriteLine(s1);        }        public void Eat(int foodCount) //吃东西        {            if (foodCount > 10) //最多吃10个            {                this.PrintSay("最多吃10个");                return;            }            FullLevel = FullLevel + foodCount;            this.PrintSay("当前可聊次数为:" + FullLevel);          }        public void Say(string str)        {            if (str == "吃")            {                this.PrintSay("请输入一个不大于10的数字!");                Eat(Convert.ToInt32(Console.ReadLine()));                return;            }            if (FullLevel <= 0)            {                this.PrintSay("不聊了,要吃东西!输入\"吃\"回车或输入Exit退出聊天");                return;            }            if (str.Contains("姓名") || str.Contains("名字"))            {                this.PrintSay("您好!\n我是机器人\n我叫" + this.Name + "\n输入Exit可以退出聊天\n我可以和你对话" + (FullLevel-1) + "次");            }            else if (str.Contains("朋友"))            {                this.PrintSay("年龄太小啦!");            }            else                                    {                this.PrintSay("你火星来的啊!");                        }            FullLevel--;            this.PrintSay("当前可聊次数为:" + FullLevel);          }    }}


 

原创粉丝点击