【Unityc#专题篇】—基础章题单实践

在这里插入图片描述


👨‍💻个人主页@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 专栏交流🧧
🟥Unity100个实战基础✨🎁
🟦 Unity100个精华一记✨🎁
🟩 Unity50个demo案例教程✨🎁
🟨 Unity100个精华细节BUG✨🎁

在这里插入图片描述

在这里插入图片描述


习题总结专题篇



🎶前言


🅰️ 题单来自:唐老狮


🎶(A)枚举


  • 实践经验

    1.三行注释的使用
    2.try catch的应用
    3.枚举和int ,string之间的转换
    4.枚举和switch之间的便捷操作

在这里插入图片描述

 class _枚举1
    {
        enum E_QQ
        {
            living,
            busy,
            leaving,
            disturb
        }
        static void Main(string[] args)
        {

            Console.WriteLine("请选择在线的状态:(0)在线,(1)忙碌,(2)离开,(3)请勿打扰");
            try
            {
                int state = int.Parse(Console.ReadLine());
                E_QQ SState = (E_QQ)state;
                Console.WriteLine("现状态为: {0}", SState);
            }
            catch
            {
                Console.WriteLine("请输入数字:");
            }
        }
    }

在这里插入图片描述

 class Program
    {
        /// <summary>
        /// 咖啡选购枚举
        /// </summary>
      enum E_coofee
        {
                Middle = 35 ,
                Big = 40 ,
                Supper = 43,
        }
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("请选择您的咖啡种类输入对应的价格:中杯(35) 大杯(40) 超大杯(43)");
                int Index = int.Parse(Console.ReadLine());
                E_coofee state = (E_coofee)Index;
                switch (state)
                {
                    case E_coofee.Middle:
                        {
                            Console.WriteLine(state);
                            break;
                        }
                    case E_coofee.Big:
                        {
                            Console.WriteLine(state);
                            break;
                        }
                    case E_coofee.Supper:
                        {
                            Console.WriteLine(state);
                            break;
                        }
                    default:
                        break;
                }
            }
            catch
            {
                Console.WriteLine("请输入咖啡的价格");
            }
        }
    }
      

在这里插入图片描述
在这里插入图片描述

   enum E_Sex
    {
        男性,
        女性
    }
    /// <summary>
    /// 职业枚举
    /// </summary>
    enum E_Work
    {
        战士,
        猎人,
        法师
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("请选择您的英雄性别: 男(0)/女(1)");
                int sex = int.Parse(Console.ReadLine());
                Console.WriteLine("请选择您的英雄职业: 战士(0)/猎人(1)/法师(2)");
                int collage = int.Parse(Console.ReadLine());

                E_Sex E_sex = (E_Sex)sex;
                E_Work E_work = (E_Work)collage;
                int damage = 0;
                int fanse = 0;
                string imagic = "";
                switch (E_sex)
                {
                    case E_Sex.男性:
                        damage += 50;
                        fanse += 100;
                        break;
                    case E_Sex.女性:
                        damage += 150;
                        fanse += 20;
                        break;
                    default:
                        break;
                }
                switch (E_work)
                {
                    case E_Work.战士:
                        damage += 20;
                        fanse += 100;
                        imagic = "冲锋";
                        break;
                    case E_Work.猎人:
                        damage += 120;
                        fanse += 30;
                        imagic = "假死";
                        break;
                    case E_Work.法师:
                        damage += 200;
                        fanse += 10;
                        imagic = "奥术冲击";
                        break;
                    default:
                        break;
                }
                Console.WriteLine("你选择了{0}{1}\n攻击为{2}\n防御力为{3}\n职业技能为{4}", E_sex, E_work, damage, fanse, imagic);
            }

            catch
            {
                Console.WriteLine("请输入对应的数字");
            }

        }
    }

在这里插入图片描述


🎶(B)一维数组


  • 实践经验

    1.简单的if else逻辑语句可以换成三目运算符
    2.foreach的灵活使用

在这里插入图片描述在这里插入图片描述

   static void Main(string[] args)
        {
            int[] A = new int[100];
            int[] B = new int[100];
            int[] C = new int[10];
            Random rand = new Random();
            //6.随机生成10个数
            for(int i = 0; i<A.Length;i++)
            {
                A[i] = 100;
                B[i] = 2 * A[i];
                if(i<C.Length )
                {

                    C[i] = rand.Next(1,101);
                }
            }
            //7.数组中找出max,min,avge,all
            int max = C[0], min = C[0], avge = 0, all = 0;
            foreach (int i in C)
            {
                Console.Write(" " + i);
                all += i;
                if (i > max) max = i;
                if (i < min) min = i;
            }
            avge = all / C.Length; 
            Console.WriteLine("\n最大值{0},最小值{1},总和{2},平均值{3}\n",max,min,all,avge);
           //8.简单反转数组
           for( int i = 0; i<C.Length /2;i++)
            {
                int temp;
                temp = C[i];
                C[i] = C[C.Length -1-i];
                C[C.Length - 1 - i] = temp;
 
            }
            foreach (int i in C)
            {
                Console.Write(" " + i);
            }
         }
             //11.打印符号
            Console.WriteLine();
            string[] array = new string[25];    //简单的if 和else可以用三目运算符代替
            for(int i = 0; i< array .Length; i++)
            {
                array[i] = i % 2 == 0 ? "*" : "&"; 
            }
            int Temp = 0;
            foreach( string i in array)
            {
                Temp++;
                Console.Write(" "+i);
                if (Temp % 5 == 0) Console.WriteLine();
            }


🎶(C)二维数组


  • 实践经验

    1.array.length 此时代表的是全部元素数量
    2.矩阵对角线规则 i == j的时候
    3.矩阵上半部规则 j >= i 的时候
  static void Main(string[] args)
        {
            #region  12
            //const int Index = 100;
            //int[,] array = new int[Index, Index];
            //int x = 1;
            //Console.WriteLine("数组长度{0}", array.Length); //此时代表整个长度
            //for (int i = 0; i < Index; i++)
            //{
            //    for (int j = 0; j < Index; j++)
            //        array[i, j] = x++;
            //}
            //foreach (int i in array) Console.Write(" " + i);
            #endregion
            #region 13
            int[,] B = new int[4, 4];
            Random rand = new Random();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    //让右上半部分为0
                    B[i, j] = j >= i ? 0 : rand.Next(1, 101);
                }
            }
            int temp = 1;
            foreach (int i in B)
            {
                Console.Write(" " + i);
                if (temp++ % 4 == 0) Console.WriteLine();
            }
            #endregion
            #region 14

            int[,] C = new int[3, 3];
            int All = 0;
            Random Rand = new Random();
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    C[i, j] = Rand.Next(1, 10);
                    if (i == j) All += C[i, j];
                }
            }
            Console.WriteLine();
            int t = 1;
            foreach (int i in C)
            {
                Console.Write(" " + i);
                if (t++ % 3 == 0) Console.WriteLine();
            }
            Console.WriteLine("对角线的和为{0}", All);
            #endregion
            #region 15
            int M,N;
            Random rrrand = new Random();
            try
            {
                Console.WriteLine("您想要要得到多少行多少列的矩阵呢");
              
                M = int.Parse(Console.ReadLine());
                N = int.Parse(Console.ReadLine());
                int[,] ros = new int[M, N];
                for (int i = 0; i < M; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        //将下标为1的行或者列全部为1
                        ros[i, j] = i == 1 || j == 1 ? 1 : rrrand.Next(0, 2);
                    }
                }
                Console.WriteLine();
                int q = 1;
                foreach (int i in ros)
                {
                    Console.Write(" " + i);
                    if (q++ % N == 0) Console.WriteLine();
                }
             
            }
          
          catch
            {
                Console.WriteLine("请输入整形数字");
            }
            #endregion

在这里插入图片描述


🎶(D)值,引用类型存储上的区别


  • 实践经验

    1.F9断点调试和F10向下调式
    2.数组和string空间开辟和引用的关系

  • 值类型的实例化
    在这里插入图片描述

  • 引用类型的实例化
    在这里插入图片描述

在这里插入图片描述

  • 特殊引用类型的实例化
    在这里插入图片描述

🎶(E)函数的使用


  • 实践经验

    1三目运算符的便捷性-简化和可观化代码
    2.三行注释的运用
    在这里插入图片描述

    /// <summary    >
    /// 函数的运用
    /// </summary>
    class Program
    {
        const float Pi = 3.1415f;
        /// <summary>
        /// 返回最大值
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
         public int MAX(int a , int b) 
        {
            return a > b ? a : b;
        }
        /// <summary>
        ///  计算圆面积和周长
        /// </summary>
        /// <param name="r"></param>
        public void Circrl( int r)
        {
            Console.WriteLine("圆的面积为{0},周长为{1}", Pi * r * r, 2 * Pi * r);
        }
        /// <summary>
        /// 数组的基本操作
        /// </summary>
        /// <param name="a"></param>
        public void Array(int [] a)
        {
            int max, min, all= 0;
            max = min = a[0];
            foreach(int i in a)
            {
                max = i > max ? i : max;
                min = i < min ? i : min;
                all += i;
            }
            Console.WriteLine("数组的最大值为{0},最小值为{1},总和为{2},平均值为{3}", max, min, all, all / a.Length);
        }
        /// <summary>
        /// 判断质数
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public bool ZhiShu( int x )
        {
            for (int i = 2; i < x; i++)
            {
                return i % 2 == 0 ? false : true;
            }
            return true;
        }
        /// <summary>
        /// 判断是否是闰年
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public bool RYear( int x )
        {
            return x % 400 == 0 || (x % 4 == 0 && x % 100 != 0) ? true : false;
        }
        static void Main(string[] args)
        {
            Program text = new Program();
            text.MAX(5, 8);
            text.Circrl(2);
            text.Array(new int[] { 1, 2, 3, 4, 5 });
            text.ZhiShu(5);
            text.RYear(2023);

        }

🎶(F)ref和out


  • 实践经验

    1.ref和out的作用是什么——参数变成引用的你变我也变
    2.ref和out的区别是什么

ref传之前不用初始化,out需要
ref传之后要在函数中被赋值,out不用

  /// <summary>
    /// ref out的区别在于,ref修饰的传参不用经过初始化但是out需要
    /// </summary>
    class Loading
    {
       private string acount = "L24158";
       private string SSSecret ="qbz123";
       public bool Message(  string a , string b, ref string c)
        {
            if(a!=acount)
            {
                c = "用户名错误";
                return false;
            }
             if(b!= secret)
            {
                c = "密码错误";
                return false;
            }
            return true;
        }
        static void Main(string[] args)
        {
            Loading text = new Loading();
            Console.WriteLine("请输入用户名:");
            string A = Console.ReadLine();
            Console.WriteLine("请输入密码:");
            string B = Console.ReadLine();
            string C = "";
           text. Message( A, B,ref C);
            Console.WriteLine(C);

        }

🎶(G)params


  • 实践经验

    1.要考虑数组为空时的条件
    2.params的特点

在这里插入图片描述

      #region 2829,param加长参数的考察
        public void Result(params int [] arrary)
        {

            int all = 0, avage = 0 , dou = 0, sin = 0;
            //要考虑全面还需判断是否为空
            if(arrary == null) { Console.WriteLine("未有参数传递进去!"); }
            for (int i = 0; i < arrary.Length; i++)
            {
                all +=arrary[i];
            }
            avage = all / arrary.Length;
            Console.WriteLine("总值为{0},平均值为{1}",all,avage);
            foreach (var item in arrary)
            {
                if (item % 2 == 0) dou += item;
                else sin += item;
            }
            Console.WriteLine("偶数和为{0}。奇数和为{1}", dou, sin);
        }

        #endregion

🎶(H)重载


  • 实践经验

    1.掌握重载的特点
    不考虑返回值
    类型一致,数量,和参数类型不一致
    在这里插入图片描述
       #region 2829,param加长参数的考察
        public void Result(params int [] arrary)
        {

            int all = 0, avage = 0 , dou = 0, sin = 0;
            //要考虑全面还需判断是否为空
            if(arrary == null) { Console.WriteLine("未有参数传递进去!"); }
            for (int i = 0; i < arrary.Length; i++)
            {
                all +=arrary[i];
            }
            avage = all / arrary.Length;
            Console.WriteLine("总值为{0},平均值为{1}",all,avage);
            foreach (var item in arrary)
            {
                if (item % 2 == 0) dou += item;
                else sin += item;
            }
            Console.WriteLine("偶数和为{0}。奇数和为{1}", dou, sin);
        }

        #endregion
        /// <summary>
        /// 考察重载的特点
        /// </summary>
        public float Result( float A ,float B)
        {
            return A > B ? A : B;
        }
        static void Main(string[] args)
        {
            int a = 1, b = 2, c = 3, d = 4;
            Program text = new Program();
            text.Result(a, b, c, d);
        }
    }

🎶(I)递归


  • 实践经验

    1.退出条件
    2.递归条件
    3.有返回值就代表结束了,不用再添加判断语句

在这里插入图片描述

   #region 32~36递归题目
        public int all;
        /// <summary>
        /// 32
        /// </summary>
        /// <param name="i"></param>
        public void Digui(int i)
        {
            if (i == 11) return; //结束条件
            Console.Write(i+" ");
            Digui(++i);           //递归条件
        }
        /// <summary>
        /// 33
        /// </summary>
        /// <param name="x"></param>
        public int CC(int w)
        {
            if (w == 0) return 1;  //结束条件
            return  w* CC(w - 1);//递归条件
        }
        public int  Mulitiplay(int x)
        {
            if(x == 1) return CC(1);   //结束条件
            return CC(x)+Mulitiplay(x-1); //递归条件
           
        }

        /// <summary>
        /// 35
        /// </summary>
        /// <param name="Alenth"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public float Length(float Alenth,int day)
        {
            if(day == 10) return Alenth;   //结束条件

            return  Length(Alenth / 2, ++day); //递归条件
        }   
        /// <summary>
        /// 36
        /// </summary>
        /// <param name="M"></param>
        public void Print(int M)
        {
            if (M > 200) return;//结束条件

            Console.Write(" " + M); Print(++M); //递归条件
        }
   
        static void Main(string[] args)
        {
            Program text = new Program();
            text.Digui(0);
            Console.WriteLine("\n"+text.Mulitiplay(5));
            Console.WriteLine("\n" + text.all);
            Console.WriteLine(text.Length(100f,1));
            text.Print(1);
            Console.WriteLine("\n"+text.CC(5));
           
        }
        #endregion

🎶(J)结构体


  • 实践经验

    1.结构体和类的区别(初始化,内存类型,自定义自身)
    2.什么时候用结构体什么时候用累(运行效率上看)
    在这里插入图片描述
   #region  37
        struct Student
        {
            string name;
            string sex;
            int age;
            string sclass;
            string majoy;
          public  Student(string name ,string sex ,int age ,string  sclass ,string majoy)
                {
                this.name = name;
                this.sex = sex;
                this.age = age;
                this.sclass = sclass;
                this.majoy = majoy;
                }
          public void Print()
            {
                Console.WriteLine("学生姓名为{0}\n性别为{1}\n年龄为{2}\n班级为{3}\n专业为{4}", name, sex, age, sclass, majoy);
            }
        }
        static void Main(string[] args)
        {
            Student SS = new Student("张三","男",18,"一班","信息工程");
            Student MM = new Student("李四", "女", 19, "二班", "信息工程");
            SS.Print();

        }
        #endregion
        #region 41
        //struct S_SmallBoss 
        //{
        //    public string name ;
        //    public int damage;   
        //}
        //static void Main(string[] args)
        //{
        //    S_SmallBoss[] boss = new S_SmallBoss[10];
        //    for (int i = 0; i < boss.Length; i++)
        //    {
        //        boss[i].name = "小怪兽" + i;
        //        boss[i].damage = i * 10;
        //    }
        //    foreach (var item in boss)
        //    {
        //        Console.WriteLine("怪兽名字为:"+item.name+"攻击力为"+item.damage );

        //    }
        //}
        #endregion



🎶(K)冒泡和选择排序


  • 实践经验

    1.冒泡:每一轮都遍历出最大或最小的给A[i]
    2.选择:每一轮替换最大或最小的下标,不断和A[i]进行交换
    在这里插入图片描述
  /// <summary>
        /// 冒泡
        /// </summary>
        /// <param name="A"></param>
      static  public void MP( int[]A)
        {
            if(A!=null)
            {
                for (int i = 0; i < A.Length; i++)
                {
                    for (int j = i+1 ; j < A.Length; j++)
                    {
                        int temp;
                        if(A[i] > A[j])
                        {
                            temp = A[i];
                            A[i] = A[j];
                            A[j] = temp;
                        }
                    }
                }
            }
            foreach (var item in A)
            {
                Console.Write(" "+ item );
            }
           
        }
        /// <summary>
        /// 简单排序
        /// </summary>
        /// <param name="A"></param>
      static   public  void JD(int[] A)
        {
            if (A != null)
            {
                for (int i = 0; i < A.Length; i++)
                {
                    int max = i;
                    int temp ;
                    for (int j = i+1; j < A.Length; j++)
                    { 
                        if(A[j]>A[max])
                        {
                            max = j;
                            temp = A[i];
                            A[i] = A[max];
                            A[j] = temp;
                        }
                    }
                }
                Console.WriteLine();
                foreach (var item in A)
                {
                    Console.Write(" " + item);
                }

            }

⭐相关文章⭐

【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随

【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现

【2023unity游戏制作-mango的冒险】-2.始画面API制作

【2023unity游戏制作-mango的冒险】-1.场景搭建

“狂飙”游戏制作—游戏分类图鉴(网易游学)

本站最全-unity常用API大全(万字详解),不信你不收藏



你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秩沅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值