唐老狮C#进阶课程练习题(自写,如果有更好的方法欢迎讨论)(2)

第28课 匿名函数

1.写一个函数传入一个整数,返回一个函数,之后执行这个匿名函数时传入一个整数和之前那个函数传入的数相乘返回结果

 static Func<int, int> Fun01(int num)
         {
             return delegate (int a)
             {
                 return a * num;
             };
         }      

主函数调用:

Func<int,int> test = Fun01(4);
Console.WriteLine(test(5)); 

第30课 Lambda表达式与闭包

1.有一个函数,会返回一个委托函数,这个委托函数中只有一句打印代码之后执行返回的委托函数时,可以打印出1~10

static Func<Action> LambdaTest()
        {
            return () =>
            {
                Action action =null;
                Console.WriteLine("打印代码");
                for (int i = 1; i < 11; i++)
                {
                    int index = i;
                    action += () =>
                    {
                        Console.Write(index);
                    };
                }
                return action;
            };
        }

主函数调用:

Func<Action> test = LambdaTest();
test()();

第32课 List排序

1.写一个怪物类,创建10个怪物将其添加到List中,对List列表进行排序,根据用户输入数字进行排序:1、攻击排序 2、防御排序 3、血量排序 4、反转

创建一个怪物类:

 class Monister
    {
        public int att;
        public int def;
        public int hp;
        public Monister(int att,int def,int hp)
        {
            this.att = att;
            this.def = def;
            this.hp = hp;
        }
        public void ShowInfo()
        {
            Console.WriteLine("怪物的攻击力为:{0},防御力为:{1},血量为:{2}",att,def,hp);
        }
    }

主函数进行调用,这里只演示攻击排序:

 			List<Monister> monisters = new List<Monister>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                monisters.Add(new Monister(r.Next(1, 100), r.Next(1, 100), r.Next(1, 100)));              
            }
            monisters.Sort((a,b)=> 
            {
                //此处是按照攻击力排序,其他排序只需更改此处即可,为了节省时间。此处省略
                return a.att > b.att ? -1 : 1;
            });
            foreach (var item in monisters)
            {
                item.ShowInfo();
            }

2.写一个物品类(类型,名字,品质),创建10个物品,添加到List中,同时使用类型、品质、名字疮毒进行比较,排序的权重是:类型>品质>名字长度

物品数据类:

  class ItemList
    {
        public int type;
        public string name;
        public int quality;
        public ItemList(int type,string name,int quality)
        {
            this.type = type;
            this.name = name;
            this.quality = quality;
        }
        public void ShowItemInfo()
        {
            Console.WriteLine("物品的类型是:{0},品质是:{1},名字是:{2}",type,quality,name);
        }
    }

主函数调用:

 			List<ItemList> itemLists = new List<ItemList>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                itemLists.Add(new ItemList(r.Next(1, 5), "" + i + "" + r.Next(100, 100000), r.Next(1, 10)));
            }
            itemLists.Sort((a, b) =>
            {
                if (a.type != b.type)
                {
                    return a.type > b.type ? 1 : -1;
                }
                if (a.quality != b.quality)
                {
                    return a.quality > b.quality ? 1 : -1;
                }
                return a.name.Length > b.name.Length ? 1 : -1;
            });
            foreach (var item in itemLists)
            {
                item.ShowItemInfo();
            }

运行结果:

物品的类型是:1,品质是:3,名字是:136130
物品的类型是:1,品质是:4,名字是:760853
物品的类型是:2,品质是:1,名字是:349943
物品的类型是:2,品质是:1,名字是:261133
物品的类型是:2,品质是:3,名字是:919746
物品的类型是:2,品质是:6,名字是:020356
物品的类型是:2,品质是:7,名字是:419499
物品的类型是:3,品质是:1,名字是:587941
物品的类型是:3,品质是:4,名字是:832324
物品的类型是:4,品质是:7,名字是:672702

3.请尝试利用List排序方式对Dictionary中的内容排序提示:得到Dictionary的所有键值对信息存入List中

 			Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(2, "456");
            dic.Add(6, "564");
            dic.Add(1, "456");
            dic.Add(4, "156453");
            dic.Add(3, "15343");
            dic.Add(5, "12233");

            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();

            foreach (KeyValuePair<int, string> item in dic)
            {
                list.Add(item);                
            }

            list.Sort((a, b) =>
            {
                return a.Key > b.Key ? 1 : -1;
            });

            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i].Key + "_" + list[i].Value);
            }

第34课 协变与逆变

1.请描述协变逆变有什么作用

(此处参考@四月的白羊座)

            //请描述协变逆变有什么作用

            //是用来修饰泛型替代符的  泛型委托和泛型接口中
            //1.out修饰的泛型类型 只能作为返回值类型 协变
            //  in修饰的泛型类型 只能作为参数类型   逆变

            //2.遵循里氏替换原则 用out和in修饰的泛型委托 如果类型是父子关系 那么可以相互装载
            // 协变: 父类泛型委托容器可以装 子类泛型委托容器 
            // 逆变: 子类泛型委托容器可以装 父类泛型委托容器

2.通过代码说明协变和逆变的作用

	//通过代码说明协变和逆变的作用
    //协变
    delegate T TestOut<out T>();
    //逆变
    delegate void TestIn<in T>(T v);

    class Father
    {

    }
    class Son:Father
    {

    }
    class Program
    {
        static void Main(string[] args)
        {
            //协变 代码
            TestOut<Son> ts = () =>
            {
                return new Son();
            };

            TestOut<Father> tf = ts;

            Father f = tf();

            //逆变 代码
            TestIn<Father> tif = (value) =>
            {

            };

            TestIn<Son> tis = tif;

            tis(new Son());
        }
    }

第36课 多线程

1.贪吃蛇中蛇的移动实现

    enum E_MoveDir
    {
        Up,
        Down,
        Right,
        Left,
    }
    class Icon
    {
        //当前移动的方向
        public E_MoveDir dir;
        //当前位置
        public int x;
        public int y;
        public Icon(int x, int y, E_MoveDir dir)
        {
            this.x = x;
            this.y = y;
            this.dir = dir;
        }

        //移动
        public void Move()
        {
            switch (dir)
            {
                case E_MoveDir.Up:
                    y -= 1;
                    break;
                case E_MoveDir.Down:
                    y += 1;
                    break;
                case E_MoveDir.Right:
                    x += 2;
                    break;
                case E_MoveDir.Left:
                    x -= 2;
                    break;
            }
        }

        //绘制
        public void Draw()
        {
            Console.SetCursorPosition(x, y);
            Console.Write("■");
        }
        //擦除
        public void Clear()
        {
            Console.SetCursorPosition(x, y);
            Console.Write("  ");
        }
        //转向
        public void ChangeDir(E_MoveDir dir)
        {
            this.dir = dir;
        }
    }

主函数调用:

Console.CursorVisible = false;
                icon = new Icon(10, 5, E_MoveDir.Right);
                icon.Draw();

                //开启多线程
                Thread t = new Thread(NewThreadLogic);
                t.IsBackground = true;
                t.Start();

                while (true)
                {
                    Thread.Sleep(500);
                    icon.Clear();
                    icon.Move();
                    icon.Draw();
                }
            
             void NewThreadLogic()
            {
                while (true)
                {
                    switch (Console.ReadKey(true).Key)
                    {
                        case ConsoleKey.W:
                            icon.ChangeDir(E_MoveDir.Up);
                            break;
                        case ConsoleKey.A:
                            icon.ChangeDir(E_MoveDir.Left);
                            break;
                        case ConsoleKey.S:
                            icon.ChangeDir(E_MoveDir.Down);
                            break;
                        case ConsoleKey.D:
                            icon.ChangeDir(E_MoveDir.Right);
                            break;
                    }
                }

第38课 预处理命令

1.请说出至少4种预处理器指令

            //请说出至少4种预处理器指令
            //#define 定义一个符号 (没有值的变量)
            //#undef 取消定义一个符号

            //#if
            //#elif
            //#else
            //#endif

            //#warning
            //#error

2.请使用预处理器指令实现 写一个函数计算两个数 当是Unity5版本时算加法 是Unity2017版本时算乘法 当时Unity2020版本时算减法 都不是返回0


 static int Calc(int a, int b)
        {
#if Unity5
            return a + b;
#elif Unity2017
            return a * b;
#elif Unity2020
            return a - b;
#else
            return 0;
#endif
        }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换? 2. 用方法来实现:将上题封装一个方法来做,方法有两个参数分别为num1,num2,将num1与num2交换。提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref。(*) 3. 请用户输入一个字符串,计算字符串中的字符个数,并输出。 4. 用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:使用可变参数,params)。 5. 用方法来实现:计算1-100之间的所有整数的和。 6. 用方法来实现:计算1-100之间的所有奇数的和。 7. 用方法来实现:判断一个给定的整数是否为“质数”。 8. 用方法来实现:计算1-100之间的所有质数(素数)的和。 9. 用方法来实现:有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并输出。不能调用数组的Max()方法。 10. 用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。 11. 用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。Math.Round() 12. 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。 13. 有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。②扩展(*):统计出每个字符的出现次数。 14. 将字符串" hello world,你 好 世界 ! "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"。 15. 制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。效果如图:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值