• 3.C#面向对象基础聊天机器人


    基于控制台的简单版的聊天机器人,词库可以自己添加。

    聊天机器人1.0版本

    源码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 面向对象聊天机器人1
    {
        class Program
        {
            static void Main(string[] args)
            {
                机器人 r1 = new 机器人();
                r1.Name = "小I";
                r1.Eat(5);
                r1.SayHello();
                while (true)
                {
                    string str = Console.ReadLine();
                    r1.Speak(str);
                }
            }
        }
        class 机器人
        {
            public string Name { get; set; }
    
            private int Fullevel { get; set; }//饥饿的程度自己知道,所以应该设为私有的属性。
    
            public void SayHello()//可以说话
            {
                Console.WriteLine("		【机器人】你好,我叫{0},很高兴认识你!", Name);
            }
            public void Eat(int foodCount)
            {
                if (Fullevel > 100)
                {
                    return;
                }
                Fullevel = Fullevel + foodCount;
            }
    
            public void Speak(String str)
            {
                if (Fullevel <= 0)
                {
                    Console.WriteLine("		【机器人】饿死了,别问了,我不说了!");
                    return;
                }
                if (str.Contains("姓名") || str.Contains("名字") || str.Contains("") || str.Contains("你好"))
                {
                    this.SayHello();//类的方法调用同类的另外一个方法
                }
                else if (str.Contains("女朋友"))
                {
                    Console.WriteLine("		【机器人】年龄小,不考虑!");
                }else if (str.Contains("创造") )
                {
                    Console.WriteLine("		【机器人说】是星云创造的我。");
    
                }else if(str.Contains("逗比"))
                {
                    Console.WriteLine("		【机器人】你才是逗比,你一家人都是逗比!");
    
                }else if (str.Contains("呵呵"))
                {
                    Console.WriteLine("		【机器人】嘻嘻");
    
                }else if (str.Contains("。。。"))
                {
                    Console.WriteLine("		【机器人】。。。。");
    
                }
                else if (str.Contains(""))
                {
                    Console.WriteLine("		【机器人】你要擦什么?");
    
                }else
                {
                    Console.WriteLine("		【机器人】听不懂!");
                }
                Fullevel--;
            }
        }
    }

    运行截图:


     


    聊天机器人2.0版本,可选择聊天机器人。

    源码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 面向对象聊天机器人2
    {
        class Program
        {
            static void Main(string[] args)
            {
                机器人 r1 = new 机器人();
                r1.Name = "小I";
                r1.Eat(5);
               
                
                机器人 r2 = new 机器人();
                r2.Name = "小J";
                r2.Eat(8);
                Console.WriteLine("请选择机器人,1——>小I,2——>小J");
                机器人 r;
                string str =Console.ReadLine();
                if(str =="1")
                {
                    r=r1;//r指向r1指向的对象
                }
                else
                {
                    r=r2;
                }
                r.SayHello();
                while (true)
                {
                    string str1 = Console.ReadLine();
                    r.Speak(str1);
                }
            }
        }
        class 机器人
        {
            public string Name { get; set; }
    
            private int Fullevel { get; set; }//饥饿的程度自己知道,所以应该设为私有的属性。
    
            public void SayHello()//可以说话
            {
                Console.WriteLine("		【机器人】你好,我叫{0},很高兴认识你!", Name);
            }
            public void Eat(int foodCount)
            {
                if (Fullevel > 100)
                {
                    return;
                }
                Fullevel = Fullevel + foodCount;
            }
    
            public void Speak(String str)
            {
                if (Fullevel <= 0)
                {
                    Console.WriteLine("		【机器人】饿死了,别问了,我不说了!");
                    return;
                }
                if (str.Contains("姓名") || str.Contains("名字") || str.Contains("") || str.Contains("你好"))
                {
                    this.SayHello();//类的方法调用同类的另外一个方法
                }
                else if (str.Contains("女朋友"))
                {
                    Console.WriteLine("		【机器人】年龄小,不考虑!");
                }else if (str.Contains("创造") )
                {
                    Console.WriteLine("		【机器人说】是星云创造的我。");
    
                }else if(str.Contains("逗比"))
                {
                    Console.WriteLine("		【机器人】你才是逗比,你一家人都是逗比!");
    
                }else if (str.Contains("呵呵"))
                {
                    Console.WriteLine("		【机器人】嘻嘻");
    
                }else if (str.Contains("。。。"))
                {
                    Console.WriteLine("		【机器人】。。。。");
    
                }
                else if (str.Contains(""))
                {
                    Console.WriteLine("		【机器人】你要擦什么?");
    
                }else
                {
                    Console.WriteLine("		【机器人】听不懂!");
                }
                Fullevel--;
            }
        }
    }

    运行结果:

     

  • 相关阅读:
    H3C S3600-28TP-SI配置命令
    笔记本双网卡内外同上
    计算机存储单位与宽带单位
    linux 设置网卡
    linux 挂载命令详解
    vue实现京东动态楼层效果
    js字符与ASCII码互转的方法
    UartAssist串口调试工具
    高字节和低字节是什么意思?
    int16, int32, int64等类型说明
  • 原文地址:https://www.cnblogs.com/xingyunblog/p/3886690.html
Copyright © 2020-2023  润新知