C#面对对象机器人

vip_rain 2013-05-21 02:33:09
用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--;

}
}
}


...全文
81 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
小Q不系萌小Q- 2013-05-21
  • 打赏
  • 举报
回复
你把代码改成下面的试试看
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;
 
        }
    }
}
Trust-Me 2013-05-21
  • 打赏
  • 举报
回复

//简单的修改了一下
 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--;

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

这个地方改一下:
while (机器人.FullLevel>=0)
            {
                string str = Console.ReadLine();
                K1.Speak(str);
            }
这个地方改一下,试试
public static int FullLevel;
  • 打赏
  • 举报
回复
private int FullLevel { get; set; } 私有属性还有什么意义吗? 1.开放FullLevel属性,通过FullLevel的值决定是否跳出循环。 2. Speak(str) 方法返回标识值,比如正常情况返回0, 饿死时返回-1, 然后利用返回值判断: if(K1.Speak(str)<0)break;

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧