用C#编写一个关于面向对象的聊天机器人,请跟以下程序,饿死以后,如果跳出循环?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 面向对象版聊天机器人
{
    class Program
    {
        static void Main(string[] args)
        {
            机器人 K1 = new 机器人();
            K1.Name = "小K";
            K1.Eat(5);
            K1.SayHello();
            while (true)
            {
                string str = Console.ReadLine();
                K1.Speak(str);
            }
            Console.ReadKey();
        }
    }
    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("饿死了,不说了");
            
            }
                            if (str.Contains("姓名") || str.Contains("名字"))
            {
                this.SayHello();
            }
            else if (str.Contains("女朋友"))
            {
                Console.WriteLine("年龄小,不考虑!");
            }
            else
            {
                Console.WriteLine("听不懂");
            }
            FullLevel--;        }
    }
}
C#机器人聊天工具

解决方案 »

  1.   

    private int FullLevel { get; set; }
    私有属性还有什么意义吗?1.开放FullLevel属性,通过FullLevel的值决定是否跳出循环。
    2. Speak(str) 方法返回标识值,比如正常情况返回0, 饿死时返回-1, 然后利用返回值判断:
    if(K1.Speak(str)<0)break; 
      

  2.   

    你想跳出?
    这个地方改一下:
    while (机器人.FullLevel>=0)
                {
                    string str = Console.ReadLine();
                    K1.Speak(str);
                }
    这个地方改一下,试试
    public static int FullLevel;
      

  3.   


    //简单的修改了一下
     class Program
        {
            static void Main(string[] args)
            {
                机器人 K1 = new 机器人();
                K1.Name = "小K";
                K1.Eat(5);
                K1.SayHello();
                while (机器人.FullLevel>=0)
                {
                    string str = Console.ReadLine();
                    K1.Speak(str);
                }
                Console.ReadKey();
            }
        }    class 机器人
        {
            public string Name { get; set; }        public static int FullLevel;        public void SayHello()
            {
                Console.WriteLine("你好,我叫:{0},有什么想问的吗?", Name);
                Console.WriteLine("");
            }        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("年龄小,不考虑!");
                        Console.WriteLine("");
                    }
                    else
                    {
                        Console.WriteLine("听不懂");
                        Console.WriteLine("");
                    }
                   
                }            FullLevel--;        }
        }
      

  4.   

    你把代码改成下面的试试看
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace 面向对象版聊天机器人
    {
        class Program
        {
            static void Main(string[] args)
            {
                机器人 K1 = new 机器人();
                K1.Name = "小K";
                K1.Eat(5);
                K1.SayHello();
                while (true)
                {
                    string str = Console.ReadLine();
                   if(K1.Speak(str)<0){
                       break;
                    }
                }
                Console.ReadKey();
            }
        }
        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 int Speak(string str)
            {
                if (FullLevel <= 0)
                {
                    Console.WriteLine("饿死了,不说了");
                    return -1;
                }
                     
     
                if (str.Contains("姓名") || str.Contains("名字"))
                {
                    this.SayHello();
                }
                else if (str.Contains("女朋友"))
                {
                    Console.WriteLine("年龄小,不考虑!");
                }
                else
                {
                    Console.WriteLine("听不懂");
                }
                FullLevel--;
                return FullLevel;
     
            }
        }
    }