机器人对话小程序
这次来个机器人的程序,代码不多,供和我一样的初学者讨论,大神勿喷~
这个程序一共有三个类,分别是Program.cs Robot.cs ShowFunction.cs
下面分别上代码:
1 namespace Robot 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 int hugerLevel=5; 8 int sum = 0; 9 ShowFuction sh = new ShowFuction(); 10 sh.showInfo(); 11 Console.WriteLine("请先选择你的机器人:"); 12 hungry robot = new hungry(); 13 14 //选择机器人 15 while(true) 16 { 17 robot.Name = Console.ReadLine(); 18 //string robotName=Console.ReadLine(); 19 if (robot.Name.Contains("#help")) 20 { 21 sh.help(robot.Name); 22 Console.WriteLine("请先选择机器人,再进行其他的操作。"); 23 } 24 else if (robot.Name.Contains("张三")) 25 { 26 Console.WriteLine("你选择的机器人是:{0}", robot.Name); 27 break; 28 } 29 else if (robot.Name.Contains("李四")) 30 { 31 Console.WriteLine("你选择的机器人是:{0}", robot.Name); 32 break; 33 } 34 else 35 { 36 Console.Clear(); 37 sh.showInfo(); 38 Console.WriteLine("你输入的有误,请查看帮助并重新选择!"); 39 } 40 } 41 42 //下面是执行各种操作的程序 43 while (true) 44 { 45 46 Console.WriteLine("请输入你想执行的操作:"); 47 string str = Console.ReadLine(); 48 if (str.Contains("#help")) 49 { 50 sh.help(str); 51 hugerLevel--; 52 robot.showHunger(hugerLevel); 53 54 } 55 else if (str.Contains("吃饭")||str.Contains("喂食")||str.Contains("饭")) 56 { 57 robot.eatLevel(hugerLevel); 58 hugerLevel++; 59 sum--; 60 robot.showHunger(hugerLevel); 61 62 } 63 else if (str.Contains("退出")) 64 { 65 Console.Clear(); 66 sh.showEnd(); 67 Console.WriteLine("拜拜咯~~"); 68 robot.exit(); 69 break; 70 } 71 else if (str.Contains("名字")||str.Contains("姓名")) 72 { 73 robot.showName(); 74 hugerLevel--; 75 robot.showHunger(hugerLevel); 76 if ((hugerLevel == 0 )||(hugerLevel>0&& hugerLevel <= 3 )|| (hugerLevel >=8 && hugerLevel<=10 )||(hugerLevel>15)) 77 { 78 robot.eatLevel(hugerLevel); 79 } 80 sum++; 81 82 } 83 else if (str.Contains("女朋友") || str.Contains("女友")) 84 { 85 robot.showGirl(); 86 hugerLevel--; 87 robot.showHunger(hugerLevel); 88 if ((hugerLevel == 0) || (hugerLevel > 0 && hugerLevel <= 3) || (hugerLevel >= 8 && hugerLevel <= 10) || (hugerLevel > 15)) 89 { 90 robot.eatLevel(hugerLevel); 91 } 92 93 } 94 else if (str.Contains("年龄") || str.Contains("岁数")||str.Contains("岁")) 95 { 96 robot.showAge(); 97 hugerLevel--; 98 robot.showHunger(hugerLevel); 99 if ((hugerLevel == 0) || (hugerLevel > 0 && hugerLevel <= 3) || (hugerLevel >= 8 && hugerLevel <= 10) || (hugerLevel > 15)) 100 { 101 robot.eatLevel(hugerLevel); 102 } 103 sum++; 104 105 } 106 else if (str.Contains("冰激凌") || str.Contains("雪糕")) 107 { 108 robot.showIce(); 109 hugerLevel--; 110 robot.showHunger(hugerLevel); 111 if ((hugerLevel == 0) || (hugerLevel > 0 && hugerLevel <= 3) || (hugerLevel >= 8 && hugerLevel <= 10) || (hugerLevel > 15)) 112 { 113 robot.eatLevel(hugerLevel); 114 } 115 sum++; 116 117 } 118 119 if (sum >= 3) 120 { 121 Console.WriteLine("你怎么这么爱问问题呀~~我都累的不行了,我要吃饭,我要吃饭!"); 122 } 123 124 } 125 126 127 Console.ReadKey(); 128 } 129 } 130 }
1 namespace Robot 2 { 3 /// <summary> 4 ///写一个智能机器人,它有姓名和饥饿级别。按#help能查看帮助 5 ///能吃饭,每吃饭一次饥饿级别+10; 6 ///能回答问题,每回答一个问题,饥饿级别-3; 7 /// </summary> 8 class hungry 9 { 10 ShowFuction shfunction = new ShowFuction(); 11 12 string name; 13 14 public string Name 15 { 16 get 17 { 18 if (name.Contains("张三")) 19 { 20 name = "张三"; 21 } 22 else if (name.Contains("李四")) 23 { 24 name = "李四"; 25 } 26 return name; 27 } 28 set { name = value; } 29 } 30 int hugerLevel; 31 32 public int HugerLevel 33 { 34 get { return hugerLevel; } 35 set { hugerLevel = value; } 36 } 37 38 public void eatLevel(int level) 39 { 40 41 if (level ==0) 42 { 43 Console.WriteLine("我已经不行了,都怪你不给我饭吃~~~"); 44 shfunction.showEnd(); 45 exit(); 46 } 47 else if (level <= 3 && level >0 ) 48 { 49 Random r = new Random(); 50 int num=r.Next(1, 3); 51 switch (num) 52 { 53 case 1: 54 Console.WriteLine("饿呀,我要吃日本料理,台湾牛肉面,重庆火锅,还有。。。嗯。。。我想想啊。。"); 55 break; 56 case 2: 57 Console.WriteLine("上午运动太多,没吃饱饭"); 58 break; 59 } 60 61 } 62 else if (level > 8 && level <= 10) 63 { 64 Console.WriteLine("我吃撑了,不能在吃了"); 65 } 66 else if (level > 15) 67 { 68 Console.WriteLine("你要是再喂我吃饭,我就OVER了!!不要啊"); 69 } 70 else 71 { 72 Console.WriteLine("你终于给我饭吃了,吼吼,吃饭真好"); 73 } 74 } 75 76 public void exit() 77 { 78 Environment.Exit(0); 79 } 80 81 public void showName() 82 { 83 Console.Clear(); 84 shfunction.help("#help"); 85 Console.WriteLine("你的记性怎么这么差呀,不是说了我叫{0}了吗。",this.name); 86 } 87 88 public void showHunger(int jiE) 89 { 90 Console.WriteLine("当前饥饿值为:{0}",jiE); 91 } 92 93 public void showGirl() 94 { 95 Console.Clear(); 96 shfunction.help("#help"); 97 Random r=new Random(); 98 int num=r.Next(1,4); 99 switch (num) 100 { 101 case 1: 102 Console.WriteLine("你真讨厌,人家还小呢,怎么会有女朋友呢?~~(*^__^*) 嘻嘻……"); 103 break; 104 case 2: 105 Console.WriteLine("当有有了,你猜猜是谁?你猜不到吗?要不要我告诉你呀?"); 106 break; 107 case 3: 108 Console.WriteLine("%>_<%~~~呜呜~~~还没有呢。。。。。。。。。"); 109 break; 110 } 111 } 112 113 public void showAge() 114 { 115 Console.Clear(); 116 shfunction.help("#help"); 117 Console.WriteLine("我今年18了!!!!"); 118 } 119 120 public void showIce() 121 { 122 Console.Clear(); 123 shfunction.help("#help"); 124 Random r = new Random(); 125 int num = r.Next(1, 4); 126 switch (num) 127 { 128 case 1: 129 Console.WriteLine("我最爱吃草莓口味的冰激凌了~~~很可爱的颜色呢~~嘻嘻"); 130 break; 131 case 2: 132 Console.WriteLine("我喜欢吃巧克力口味的,因为它看上去很有食欲"); 133 break; 134 case 3: 135 Console.WriteLine("我喜欢芒果味道的,你喜欢吃什么味道的呢?我猜是葡萄口味的对不对?"); 136 break; 137 138 } 139 } 140 141 142 143 } 144 }
1 namespace Robot 2 { 3 class ShowFuction 4 { 5 public void showInfo() 6 { 7 Console.WriteLine("*****************************************************"); 8 Console.WriteLine("写一个智能机器人,它有姓名和饥饿级别。按#help能查看帮助"); 9 Console.WriteLine("能吃饭,每吃饭一次饥饿级别+10;"); 10 Console.WriteLine("能回答问题,每回答一个问题,饥饿级别-3;"); 11 Console.WriteLine("*****************************************************"); 12 } 13 14 //按#help将打开帮助菜单 15 public void help(string info) 16 { 17 18 if (info.Contains("#help")) 19 { 20 Console.Clear(); 21 Console.WriteLine("*************************************************************************"); 22 Console.WriteLine("一、机器人信息选择:"); 23 Console.Write("请选择机器人:1-----张三 2-----李四\n\r"); 24 Console.WriteLine(); 25 Console.WriteLine("*************************************************************************"); 26 Console.Write("二、机器人对话:\n\r"); 27 Console.Write("1--你要吃饭吗?"); 28 Console.Write(" ");//3个tab 29 Console.WriteLine("2--你有女朋友吗?"); 30 Console.WriteLine(); 31 Console.Write("3--你叫什么名字?"); 32 Console.Write(" ");//3个tab 33 Console.WriteLine("4--你今年几岁了?"); 34 Console.WriteLine(); 35 Console.WriteLine("5--你最爱吃什么口味的冰激凌?\n\r"); 36 Console.WriteLine("*************************************************************************"); 37 Console.WriteLine("三、喂食:"); 38 Console.WriteLine("输入“喂食”两个字,会给机器人喂食,喂食后会显示饥饿等级,并输出饥饿状态"); 39 Console.WriteLine("*************************************************************************"); 40 Console.WriteLine("四、退出"); 41 Console.WriteLine("*************************************************************************"); 42 43 } 44 } 45 46 public void showEnd() 47 { 48 Console.WriteLine("*************************************************************************"); 49 Console.WriteLine("*************************我 要 退 出 了**************************"); 50 Console.WriteLine("*************************************************************************"); 51 } 52 } 53 }
最后在来三张程序运行的截图: