黑马程序员_练习聊天程序员

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

//2014.3.13

 

namespace 练习聊天机器人

{

    class Program

    {

        static void Main(string[] args)

        {

            机器人 r1 = new 机器人();

            r1.Name = "T300";

            r1.Eat(6);

 

            机器人 r2 = new 机器人();

            r2.Name = "G500";

            r2.Eat(8);

            Console.WriteLine("请选择机器人,1-T300,2-G500");

            机器人 r;

            string s=Console.ReadLine();

 

            if (s == "1")

            {

                r = r1;

            }

            else if (s == "2")

            {

                r = r2;

            }

            else

            {

                Console.WriteLine("输入错误");

                Console.ReadKey();

                return;

            }

              r.SayHello();

            while (true)

            {

                string str = Console.ReadLine();

                r.Speak(str);

            }

        }

    }

 

    class 机器人

    {

        public string Name

        {

            get;

            set;

        }

        private int FullLevel//饥饿程度

        {

            get;

            set;

        }

 

        public void SayHello()

        {

            Console.WriteLine("我叫{0}",Name);

        }

 

        public void Eat(int foodCount)

        {

            if (FullLevel>100)

            {

                return;

            }

            FullLevel = FullLevel + foodCount;

        }

 

        public void Speak(string str)

        {

            if (FullLevel <= 0)

            {

                Console.WriteLine("饿死了,不说了");

            }

            else if (str.Contains("名字") || str.Contains("姓名"))

            {

                this.SayHello();

            }

            else if (str.Contains("女朋友"))

            {

                Console.WriteLine("年龄小,不考虑");

            }

            else

            {

                Console.WriteLine("听不懂!");

            }

            FullLevel--;

        }

    }

}

你可能感兴趣的:(程序员)