{
            机器人 p1 = new 机器人();
            p1.Nane="小I";
            p1.Eat(5);
            机器人 p2 = new 机器人();
            p2.Nane = "小J";
            p2.Eat(3);
            Console.WriteLine("请选择机器人:1或2,1是小I,2是小J");
            string st3 = Console.ReadLine();
            if (st3 == "1")
            {
                p1 = p2;
            }
            else
            {
                p2 = p1;
            }
            p1.SayHello();
            while(true)
            {
               string st1=Console.ReadLine();
               p1.Speak(st1);
            }
        }
    }
    class 机器人
    {
        public string Nane { get; set; }
        private int FullLevel { get; set; }
        public void SayHello()
        {
            Console.WriteLine("我叫{0}",Nane);
        }
        public void Eat(int foodConunt)
        {
            if(FullLevel>100)
            {
                return;
            }
            FullLevel = FullLevel + foodConunt;
        }
        public void Speak(string st)
        {
            if(FullLevel<=0)
            {
                Console.WriteLine("额死了 不聊了");
                string srt1 = Console.ReadLine();
                int food = Convert.ToInt32(srt1);
                if(food<=0)
                {
                    Console.WriteLine("兄弟你玩我的吧");
                    return;
                }
            }
            if (st == "你好~~~")
            {
                Console.WriteLine("我今天心情不是很好别来烦我");
            }
            else if (st == "88")
            {
                Console.WriteLine("再见");
                return;
            }
            else
            {
                Console.WriteLine("你说什么,我听不懂");
            }
            FullLevel--;
        }
还有这段代码                if(food<=0)
                {
                    Console.WriteLine("兄弟你玩我的吧");
                    return;
                }
也结束不了 要怎么才能实现???

解决方案 »

  1.   

    你Speak函数虽然return了,但是你while循环没结束,当然退步了程序了
    把你Speak函数改造一下就可以了 static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                机器人 p1 = new 机器人();
                p1.Nane = "小I";
                p1.Eat(5);
                机器人 p2 = new 机器人();
                p2.Nane = "小J";
                p2.Eat(3);
                Console.WriteLine("请选择机器人:1或2,1是小I,2是小J");
                string st3 = Console.ReadLine();
                if (st3 == "1")
                {
                    p1 = p2;
                }
                else
                {
                    p2 = p1;
                }
                p1.SayHello();
                while (true)
                {
                    string st1 = Console.ReadLine();
                    if (p1.Speak(st1) == true)  //Speak函数返回true了,结束程序
                        break;
                }
            }
        }
        class 机器人
        {
            public string Nane { get; set; }
            private int FullLevel { get; set; }
            public void SayHello()
            {
                Console.WriteLine("我叫{0}", Nane);
            }
            public void Eat(int foodConunt)
            {
                if (FullLevel > 100)
                {
                    return;
                }
                FullLevel = FullLevel + foodConunt;
            }
            public bool Speak(string st)
            {
                if (FullLevel <= 0)
                {
                    Console.WriteLine("额死了 不聊了");
                    string srt1 = Console.ReadLine();
                    int food = Convert.ToInt32(srt1);
                    if (food <= 0)
                    {
                        Console.WriteLine("兄弟你玩我的吧");
                        return true;
                    }
                }
                if (st == "你好~~~")
                {
                    Console.WriteLine("我今天心情不是很好别来烦我");
                }
                else if (st == "88")
                {
                    Console.WriteLine("再见");
                    return true;  //通知while,结束
                }
                else
                {
                    Console.WriteLine("你说什么,我听不懂");
                }
                FullLevel--;
                return false;
            }    }
      

  2.   

                机器人 p1 = new 机器人();
                p1.Nane="小I";
                p1.Eat(5);
                机器人 p2 = new 机器人();
                p2.Nane = "小J";
                p2.Eat(3);
                Console.WriteLine("请选择机器人:1或2,1是小I,2是小J");
                string st3 = Console.ReadLine();
                if (st3 == "1")
                {
                    p1 = p2;
                }
                else
                {
                    p2 = p1;
                }
                p1.SayHello();
                while(true)
                {
                   string st1=Console.ReadLine();
                   if (p1.Speak(st1) == true)  //Speak函数返回true了,结束程序
                       break;
                }
            }
        }
        class 机器人
        {
            public string Nane { get; set; }
            private int FullLevel { get; set; }
            public void SayHello()
            {
                Console.WriteLine("我叫{0}",Nane);
            }
            public void Eat(int foodConunt)
            {
                if(FullLevel>100)
                {
                    return;
                }
                FullLevel = FullLevel + foodConunt;
            }
            public void Speak(string st)
            {
                if(FullLevel<=0)
                {
                    Console.WriteLine("额死了 不聊了");
                    string srt1 = Console.ReadLine();
                    int food = Convert.ToInt32(srt1);
                    if(food<=0)
                    {
                        Console.WriteLine("兄弟你玩我的吧");
                        return true;                }
                }
                if (st == "你好~~~")
                {
                    Console.WriteLine("我今天心情不是很好别来烦我");
                }
                else if (st == "88")
                {
                    Console.WriteLine("再见");
                    return true;
                }
                else
                {
                    Console.WriteLine("你说什么,我听不懂");
                }
                FullLevel--;
                return false;
    为什么我修改后提示
    错误 1 运算符“==”无法应用于“void”和“bool”类型的操作数
    错误 2 由于“机器人2.机器人.Speak(string)”返回 void,返回关键字后面不得有对象表达式
      

  3.   

     把public void SayHello()
    改成
     public bool SayHello()把return;
    改成 return true;
      

  4.   

    晕了晕了
    上面看错了
    是把 public void Speak(string st)
    改成
     public bool Speak(string st)
      

  5.   

    请问
    为什么要改成bool呢  麻烦你解释一下
      

  6.   

    bool类型的函数才能返回true或false,从而告诉调用的while循环是不是该退出