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

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

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

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);  

        }

    }

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值