using System; using System.Collections.Generic; using System.Linq; using System.Text; /* 面向对像版聊天机器人 * 机器人有不同的名字,维护自己的FullLevel,可以SayHello,可以喂食,可以对它说话(Speak),对异常情况(错误的喂饭数字,喂养的太多撑死了)进行处理,有两个机器人供选择,一开始通过1 2 数字选择聊天机器人 * * */ namespace _4练习聊天机器人 { class Program { static void Main(string[] args) { /*Person7 p = new Person7(); p.Age = 22; p.Age++; p.Name = "xxdxxd"; Console.WriteLine("Name:{0},Age:{1}",p.Name, p.Age); Console.ReadKey();*/ 机器人 r1 = new 机器人(); r1.Name = "小三"; r1.Eat(10); //r1.SayHello(); 机器人 r2 = new 机器人(); r2.Name = "小四"; r2.Eat(10); //r2.SayHello(); 机器人 r; Console.WriteLine("请选择机器人,1为'小三' 2为'小四' "); string str = Console.ReadLine(); if (str == "1") { r = r1; } else if (str == "2") { r = r2; } else { Console.WriteLine("输入错误!"); return; } r.SayHello(); while (true) { string _str = Console.ReadLine(); r1.Speak(_str); } Console.ReadKey(); } } class 机器人 { public string Name { get; set; } private int FullLevel { get; set; } public void SayHello() { Console.WriteLine("我叫:{0}",this.Name); } public void Eat(int foodCount) { if (FullLevel >= 100) { return; } FullLevel += foodCount; } public void Speak(string str) { if (FullLevel <= 0) { Console.WriteLine("不说了,饿死我了!"); return; } if (str.Contains("姓名") || str.Contains("名字")) { this.SayHello(); } else if (str.Contains("女朋友")) { Console.WriteLine("年龄小,不考虑!"); } else { Console.WriteLine("听不懂!"); } FullLevel--; } } class Person7 { //难到声明也省了???????? public int Age { get; //编辑器自动帮我们生成私有的字段和set,get代码块 set; } public string Name { get; set; } } }