我編寫的第一個程式:聊天機器人。

來源:互聯網
上載者:User

這是我編寫的第一個程式:聊天機器人。

代碼如下,非常粗陋。希望大家多多指點,謝謝!

PS:

這個聊天機器人是在看了楊中科老師的視頻後做的,思路都是課上楊老師講的,只是加了一個食譜。本以為看視頻的時候都看明白了,之後自己寫一遍不會太難。結果出乎我的意料,光DEBUG就花了很長時間。楊老師課上的一句話“程式員不是聽課聽出來的,也不是看書看出來的,而是編程編出來的”真的太有道理了。

PS:

程式中建立了一個字串數組"recipeone".eat時如果輸入字串"food"中包含"recipeone"中的某個字串,就開始eat。

於是就寫了:if (food.Contains(recipeone[]))和if (food.Contains(recipeone)),發現都不行,Contains方法不能包含字串數組。最後無可奈何,用了foreach迴圈,一個個比一下。不知道有沒有更好的辦法,希望大家能告訴我,謝謝!

 

PS:

 

我把機器人的類放到類庫項目中,主程式一個項目,輸入“88”後,

程式執行          MyFirstRobot.die();   //此方法在類庫中
                      return;

但是沒有立即退出,而是出現了“輸入任意鍵繼續。。。”,按任意一個鍵後控制台退出。

不知道是什麼原因,程式裡沒有Console.ReadKey();啊

 

 namespace oneRobot
{
    class Program
    {
        static void Main(string[] args)
        {
            //構造機器人,名字name:大熊。饑餓值hungerLevel“5”
            RobotOne MyFirstRobot = new RobotOne("大熊", 5);
            Console.WriteLine("你好!");
            //開始聊天
            while (true)
            {

                //饑餓值<0,餓死退出。 0<饑餓值<3,喊餓,進食。
                if (MyFirstRobot.hungerLevel < 0)
                {
                    MyFirstRobot.die();
                    return;
                }
                else if (MyFirstRobot.hungerLevel < 3 && MyFirstRobot.hungerLevel >= 0)
                {
                    MyFirstRobot.hungry();
                    MyFirstRobot.Eat(Console.ReadLine());

                }
               //饑餓值>=3,聊天。輸入內容含“88”或“再見”,退出。否則,用chat方法聊天。
                else
                {
                    string input = Console.ReadLine();
                    if (input.Contains("88") || input.Contains("再見"))
                    {
                        Console.WriteLine("886");
                        return;
                    }
                    else
                    {
                        MyFirstRobot.Chat(input);
                    }
                }
            }
        }
    }
    class RobotOne
    {
        //機器人食譜
        public string[] recipeOne = new string[] { "果", "香蕉", "水果", "西瓜", "草莓", "梨", "橘子",
                         "柚子", "荔枝", "甘蔗", "包子", "餃子", "驢肉火燒", "面", "米", "肉", "粥", "蔬菜" };
        string name;
        public int hungerLevel;

        //喊餓方法
        public void hungry()
        {
            Console.WriteLine("餓死啦,我要吃飯");
            hungerLevel--;
        }
        //餓死了。
        public void die()
        {
            Console.WriteLine("餓死了。。。。。");
        }
        //如果輸入中包含食譜中的內容,進食。
        public void Eat(string food)                
        {
            foreach (string str in recipeOne)
            {
                if (food.Contains(str))
                {
                    hungerLevel += 3;
                    Console.WriteLine("太好吃了!你是好人");
                    return;
                }
            }
        }
        //聊天。
        public void Chat(string talk)       
        {
            if (talk.Contains("你") && (talk.Contains("名字") || talk.Contains("叫啥") || talk.Contains("是誰")))
            {
                Console.WriteLine("我是最聰明的機器人{0}", this.name);
                hungerLevel--;
            }
            else if (talk.Contains("你好"))
            {
                Console.WriteLine("你好!");
                hungerLevel--;
            }
            else if (talk.Contains("天氣") && (talk.Contains("?") || talk.Contains("嗎") || talk.Contains("怎麼樣")))
            {
                Console.WriteLine("天氣很好!");
                hungerLevel--;
            }
            else
            {
                Console.WriteLine("聽不懂!");
                hungerLevel--;
            }
        }
        //建構函式。初始化機器人的名字,饑餓度。
        public RobotOne(string inputname, int inputhungry)            
        {
            name = inputname;
            hungerLevel = inputhungry;
        }
    }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.