吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4859|回复: 15
收起左侧

[其他转载] 我的C#之路之简单的聊天机器人。

[复制链接]
我的女神好可爱~ 发表于 2017-4-8 15:34
本帖最后由 我的女神好可爱~ 于 2017-4-8 15:37 编辑

/*
* 版权:我的女神好可爱~
* 日期:2017/4/4
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hxj.chapter3
{
    class test2
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请选择机器人:1可爱模式:2搞怪模式");
            string xuanzhe = Console.ReadLine();  //定义一个变量,来选择机器人。
            if (xuanzhe == "1")
            {
                jiqiren2 r1 = new jiqiren2();  //new 一个新的机器人
                r1.Name = "小 Q"; //赋值给机器人一个名字
                r1.FullLevel = 5;   //给机器人一个饥饿程度。
                Console.WriteLine(@"你好\微笑");
                while (true) //用while语句创建一个无限循环
                {
                    if (r1.FullLevel > 0) //判断机器人的饥饿程度,如果大于0则继续执行,
                    {
                        string str1 = Console.ReadLine();
                        r1.speak(str1);
                    }
                    else //否则运行
                    {
                        Console.WriteLine("饿死了,不说了,喂我点吃的吧。");
                        r1.Eat(); //转机器人喂食。
                    }
                }
            }
            else
            {
                jiqiren r2 = new jiqiren(); //new 一个新的机器人
                r2.Name = "小八";
                r2.I = 1;
                r2.FullLevel = 5;
                Console.WriteLine(@"你好啊\(^_^) ");
                while (true)
                {
                    if (r2.FullLevel > 0)  
                    {
                        string str1 = Console.ReadLine();
                        r2.speak(str1);
                    }
                    else
                    {
                        Console.WriteLine("饿死了,不说了,喂我点吃的吧。");
                        r2.Eat();
                    }
                }
            }
        }
    }
    class jiqiren //创建一个机器人累。
    {
        private string name; //名字

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int i;

        public int I
        {
            get { return i; }
            set { i = value; }
        }
        private int fullLevel;

        public int FullLevel
        {
            get { return fullLevel; }
            set { fullLevel = value; }
        }

        public void Eat() //喂食
        {
            Console.WriteLine("喂多少?");
            string food = Console.ReadLine();
            int foodCount = Convert.ToInt32(food);
            if (foodCount > 10)
            {
                Console.WriteLine("撑死了");
                return;
            }
            else if (foodCount <= 0)
            {
                Console.WriteLine("兄弟,你玩我?");
                return;
            }
            else
            {
                Console.WriteLine("继续聊天吧亲!");
            }
            FullLevel = FullLevel + foodCount;
        }  
        private string danshen;

        public string Danshen
        {
            get { return danshen; }
            set { danshen = value; }
        }
        public void seyhello() //打招呼。
        {
            Console.WriteLine("老子叫{0}{1}", name,danshen);
            Console.ReadLine();
            Console.WriteLine("!!!不好意思,顺口了。");
            Console.WriteLine("我叫{0},也可以叫我小爸。很高兴认识你。(@》&#1538;《@)", name);
        }
        public void speak(string str) //机器人聊天的核心
        {
            if (str.Contains("姓名") || str.Contains("名字") || str.Contains("你叫"))
            {
                this.seyhello();
            }
            else if (str.Contains("女朋友") || str.Contains("媳妇")||str.Contains("对象"))
            {
                Console.WriteLine("先别问我,先说说你,你有女朋友吗?");
                str = Console.ReadLine();
                if (str.Contains("没有") || str.Contains("没"))
                {
                    Console.WriteLine("好吧,单身狗。");
                    this.danshen = "单身狗";
                    Console.WriteLine("我还小,不考虑。");
                }
                else
                {
                    Console.WriteLine("好吧,我没有。");
                }
            }
            else if(str.Contains("你好"))
            {
                Console.WriteLine("嗯,有什么能帮助你的吗?");
            }
            else
            {
                if(i==1)
                {
                Console.WriteLine("什么啊?听不懂");
                this.i++;
                }
                else if (i == 2)
                {
                    Console.WriteLine("说了听不懂了,你是不是笨蛋啊。");
                    this.i++;
                }
                else if (i == 3)
                {
                    Console.WriteLine("好了,好了,我只有简单的一些功能,有些真的听不懂");
                    this.i++;
                }
                else
                {
                    Console.WriteLine("。。。");
                    Console.WriteLine("还是听不懂。");
                    this.i = 1;
                }

            }
            FullLevel--;//这一句是,每聊一句还,饥饿程度减1
        }
    }

    class jiqiren2  //  跟上面一样。
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int fullLevel;

        public int FullLevel
        {
            get { return fullLevel; }
            set { fullLevel = value; }
        }

        public void Eat()
        {
            Console.WriteLine("喂多少?");
            string food = Console.ReadLine();
            int foodCount = Convert.ToInt32(food);
            if (foodCount > 10)
            {
                Console.WriteLine("撑死了");
                return;
            }
            else if (foodCount <= 0)
            {
                Console.WriteLine("兄弟,你玩我?");
                return;
            }
            else
            {
                Console.WriteLine("继续聊天吧亲!");
            }
            FullLevel = FullLevel + foodCount;
        }  
        public void seyhello()
        {
            Console.WriteLine("我叫{0}", name);
        }
        public void speak(string str)
        {
            if (str.Contains("姓名") || str.Contains("名字") || str.Contains("你叫"))
            {
                this.seyhello();

            }
            else if (str.Contains("你好") || str.Contains("嗯"))
            {
                Console.WriteLine("有什么能帮助你的吗?");
                str = Console.ReadLine();
                if (str.Contains("嗯") || str.Contains("有") || str.Contains("能"))
                {
                    Console.WriteLine("有,我也不会");
                }
                else if (str.Contains("没") || str.Contains("不用") || str.Contains("好吧"))
                {
                    Console.WriteLine("好吧,哪算了");
                }
            }
            else
            {
                Console.WriteLine("听不懂耶");
            }
            FullLevel--;
        }
    }
}
大学刚下学期学的一段时间,来总结一下学到的知识。
欢迎打赏。
希望能帮助到那些刚入门的C#学者。本身我也在学习,也希望大家可以交流一下C#的学习经验。
c#本身也是我的爱好之一,希望我能坚持下去,也希望那些刚刚入门的语言学者能走下去。

点评

如果来装逼的这样就可以,如果是想让新手学习的请加多加注释,新人容易看不懂。 弄个继承会更轻松。 加油吧  发表于 2017-4-8 16:14

免费评分

参与人数 3吾爱币 +4 热心值 +3 收起 理由
xiaozhou2017 + 1 + 1 热心回复!
loooooooong + 1 + 1 接入图灵机器人可玩性性比较高
as51271239 + 2 + 1 good

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 我的女神好可爱~ 发表于 2017-4-10 17:52
roboger 发表于 2017-4-9 22:30
请问回答的数据库 在哪里建立的,有没有完成的程序,发一下 学习了。

还没有数据库,这只是好一个简单的小程序
 楼主| 我的女神好可爱~ 发表于 2017-4-8 21:32
520wangshun 发表于 2017-4-8 20:35
太多判断,影响程序性能

那应该怎么实现对话。。我现在学到的只是这些
Pythoner 发表于 2017-4-8 15:47
ziddu 发表于 2017-4-8 16:49
确实应该多一点的注释
wangsheng66 发表于 2017-4-8 16:55
一个try{}catch{}都木有。。。
520wangshun 发表于 2017-4-8 20:35
太多判断,影响程序性能
 楼主| 我的女神好可爱~ 发表于 2017-4-8 21:31
ziddu 发表于 2017-4-8 16:49
确实应该多一点的注释

对对,下次有新的程序好好注释一下
 楼主| 我的女神好可爱~ 发表于 2017-4-8 21:31
wangsheng66 发表于 2017-4-8 16:55
一个try{}catch{}都木有。。。

我是个初学者,正在努力更改,
hickey. 发表于 2017-4-9 00:21
厉害了!'
18677405259 发表于 2017-4-9 10:14
程序好好注释一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-23 17:20

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表