老鼠编程(猫抓老鼠的编程代码)

2023-12-28 15:01:38 数码极客 bianji01

 

程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)

using System;namespace CatMouseMaster///

/// public class Housepublic Cat Catreturn this.cat;public Mouse[] Mousesreturn this.mouses;public Master Masterreturn this.master;public string Placereturn this.place;private Cat cat;private Mouse[] mouses;private Master master;private string place;public House(string place, string master, string cat, params string[] mouses)this.place = place;Console.WriteLine("在{0}旁边有一栋房子", this.Place);this.master = new Master(master, this);this.cat = new Cat(cat, this);if (mouses != null)this.mouses = new Mouse[mouses.Length];for(int i = 0; i < mouses.Length; i++)this.mouses[i] = new Mouse(mouses[i], this);/// /// 应用程序的主入口点。/// [STAThread]static void Main(string[] args)House house = new House("白宫", "布什", "汤姆", "杰瑞", "莱姆", "强尼", "布莱克", "劳伦斯", "杰克", "汉克斯", "巴克", "萨姆", "山姆", "克里斯", "伍德");house.Night();Console.ReadLine();public void Night()Console.WriteLine("\n一天晚上");this.Cat.MiaoMiao();public delegate void CatMiaomiaoEventHandler(Cat cat, EventArgs args);public delegate void MouseRunEventHandler(Mouse mouse, EventArgs args);public delegate void TroubleByMouseEventHandler(Master master, Mouse mouse, EventArgs args);public delegate void MouseCatchdEventHandler(Cat cat, Mouse mouse, EventArgs args);public class Lifepublic string Namereturn name;public House Housereturn this.house;private string name;private House house;public Life(string name, House house)this.name = name;this.house = house;public class Cat : Lifepublic static event CatMiaomiaoEventHandler Miao;public Cat(string name, House house): base(name, house)Console.WriteLine("{0}养了一只小猫,名字叫做{1}", this.House.Master.Name, this.Name);public void MiaoMiao()Console.WriteLine("小猫{0}大叫一声:\"喵~~~~~\"", this.Name);if (Cat.Miao != null)Cat.Miao(this, EventArgs.Empty);public void CatchMouse(Mouse mouse)Console.WriteLine("小猫{0}抓住了老鼠{1}", this.Name, mouse.Name);mouse.CatchdByCat(this);public class Mouse : Lifepublic static int RunNumreturn runNum;private static int runNum = 0;private static int prefixFlag = 0;public static event MouseRunEventHandler Run;public event MouseCatchdEventHandler Catchd;public Mouse(string name, House house): base(name, house)if (prefixFlag == 0)Console.Write("房子里面有许多老鼠,它们是:");prefixFlag = 1;Console.Write(",");Console.Write(this.Name);Cat.Miao += new CatMiaomiaoEventHandler(Cat_Miao);this.Catchd += new MouseCatchdEventHandler(Mouse_Catchd);private void Cat_Miao(Cat cat, EventArgs args)Console.WriteLine("老鼠{0}吱吱的说:\"{1}来了,咱们快逃!\"", this.Name, cat.Name);runNum++;if (Run != null)Mouse.Run(this, EventArgs.Empty);public void CatchdByCat(Cat cat)if (Catchd != null)Catchd(cat, this, EventArgs.Empty);private void Mouse_Catchd(Cat cat, Mouse mouse, EventArgs args)Console.WriteLine("老鼠{0}对小猫{1}说:\"我现在瘦骨伶仃的,等我长肥了你再来吃我吧\"", this.Name, cat.Name);public class Master : Lifepublic Master(string name, House house): base(name, house)Console.WriteLine("住在这里的主人是{0}", this.Name);Mouse.Run += new MouseRunEventHandler(Mouse_Run);this.Troubled += new TroubleByMouseEventHandler(Master_Troubled);public event TroubleByMouseEventHandler Troubled;private void Mouse_Run(Mouse mouse, EventArgs args)if (Mouse.RunNum < 5)return;this.TroubledByMouse(mouse);public void TroubledByMouse(Mouse mouse)if (Mouse.RunNum == 5)Console.WriteLine("主人{0}被惊醒了", this.Name);Console.WriteLine("{0}大叫一声:\"吵死了啦!\"", this.Name);Console.WriteLine("{0}大声吼道:\"{1},快起来干活,不抓住第{2}只老鼠{3}我就把你扔到野外去!\"", this.Name, this.House.Cat.Name, Mouse.RunNum, mouse.Name);if (this.Troubled != null)this.Troubled(this, mouse, EventArgs.Empty);public void OrderCatToCatchMouse(Cat cat, Mouse mouse)cat.CatchMouse(mouse);private void Master_Troubled(Master master, Mouse mouse, EventArgs args)this.OrderCatToCatchMouse(this.House.Cat, mouse);在白宫旁边有一栋房子住在这里的主人是布什布什养了一只小猫,名字叫做汤姆房子里面有许多老鼠,它们是:杰瑞,莱姆,强尼,布莱克,劳伦斯,杰克,汉克斯,巴克,萨姆,山姆,克里斯,伍德小猫汤姆大叫一声:"喵~~~~~"老鼠杰瑞吱吱的说:"汤姆来了,咱们快逃!"老鼠莱姆吱吱的说:"汤姆来了,咱们快逃!"老鼠强尼吱吱的说:"汤姆来了,咱们快逃!"老鼠布莱克吱吱的说:"汤姆来了,咱们快逃!"老鼠劳伦斯吱吱的说:"汤姆来了,咱们快逃!"主人布什被惊醒了布什大叫一声:"吵死了啦!"老鼠杰克吱吱的说:"汤姆来了,咱们快逃!"布什大声吼道:"汤姆,快起来干活,不抓住第6只老鼠杰克我就把你扔到野外去!"小猫汤姆抓住了老鼠杰克老鼠杰克对小猫汤姆说:"我现在瘦骨伶仃的,等我长肥了你再来吃我吧"老鼠汉克斯吱吱的说:"汤姆来了,咱们快逃!"布什大声吼道:"汤姆,快起来干活,不抓住第7只老鼠汉克斯我就把你扔到野外去!"小猫汤姆抓住了老鼠汉克斯老鼠汉克斯对小猫汤姆说:"我现在瘦骨伶仃的,等我长肥了你再来吃我吧"老鼠巴克吱吱的说:"汤姆来了,咱们快逃!"布什大声吼道:"汤姆,快起来干活,不抓住第8只老鼠巴克我就把你扔到野外去!"小猫汤姆抓住了老鼠巴克老鼠巴克对小猫汤姆说:"我现在瘦骨伶仃的,等我长肥了你再来吃我吧"老鼠萨姆吱吱的说:"汤姆来了,咱们快逃!"布什大声吼道:"汤姆,快起来干活,不抓住第9只老鼠萨姆我就把你扔到野外去!"小猫汤姆抓住了老鼠萨姆老鼠萨姆对小猫汤姆说:"我现在瘦骨伶仃的,等我长肥了你再来吃我吧"老鼠山姆吱吱的说:"汤姆来了,咱们快逃!"布什大声吼道:"汤姆,快起来干活,不抓住第10只老鼠山姆我就把你扔到野外去!"小猫汤姆抓住了老鼠山姆老鼠山姆对小猫汤姆说:"我现在瘦骨伶仃的,等我长肥了你再来吃我吧"老鼠克里斯吱吱的说:"汤姆来了,咱们快逃!"布什大声吼道:"汤姆,快起来干活,不抓住第11只老鼠克里斯我就把你扔到野外去!"小猫汤姆抓住了老鼠克里斯老鼠克里斯对小猫汤姆说:"我现在瘦骨伶仃的,等我长肥了你再来吃我吧"老鼠伍德吱吱的说:"汤姆来了,咱们快逃!"布什大声吼道:"汤姆,快起来干活,不抓住第12只老鼠伍德我就把你扔到野外去!"小猫汤姆抓住了老鼠伍德老鼠伍德对小猫汤姆说:"我现在瘦骨伶仃的,等我长肥了你再来吃我吧"LZ说的是不是C#里面的委托吧?真是配服2楼的那位,非常有想像力。楼上的真牛,你从哪里找的?都没明白你说什么?

Learning Resources儿童STEM玩具编程老鼠能启蒙小吗?

我也不确定,还是看看专业人士怎么说。

C语言耗子问题

声明:易趣百科所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系315127732@qq.com
广告位招租
横幅广告