默认计划
2389人加入学习
(4人评价)
【唐老狮】Unity基础课程之C#基础
价格 免费
承诺服务
该课程属于 Unity3d实战就业路线课程套餐
请加入后再学习

Console.ReadKey().Key返回按键枚举

[展开全文]

1.枚举名以E 或者E_开头作为命名规范

enum E_自定义枚举名{ 1,2,3}

2.枚举定义默认值后 后面的枚举值也会随上一个基础上累加

3.枚举不能再函数语句块里声明

4.枚举多与swith配合使用

5.string转换枚举

playerType=(E_PlayerType)Enum.Parse(typeof(E_PlayerType),"Other");

[展开全文]

params 接数组

数组任意类型,实现传入n个参数

例如 static void k(params int [] a)

 

k(1, 2, 3);

函数参数中也可能有别的参数

函数参数中最多出现一个params关键字, 并且一定是在最后一组参数,前面可以有n个其他参数

[展开全文]

ref传入的变量必须初始化,out不用

out传入的变量必须在内部赋值,ref不用

[展开全文]

值类型存储在栈空间——系统分配,自动回收,小而快

 

引用类型存储在堆空间——手动申请和释放,大而慢

[展开全文]

申明枚举语法

表状态等

比如主玩家与其他玩家

enum E_自定义枚举名    /若是没有赋值,则是上一个值 ++;最开始无是0;

{

        自定义枚举项名字,

        自定义枚举项名字,

}

在namespace语句块中声明(常用)

class struct;

但是不能在函数中声明

使用时可在函数中使用

自定义的枚举类型 变量名=默认值(自定义的枚举类型,枚举项)

 

可与int强制类型转换

也可与Srting转换 ToString() 为枚举项名字

Enum.Parse(typeof(枚举类型), "用于转换的字符串(枚举项存在的)");

[展开全文]

0    10

       11

        12

22

23

24

[展开全文]

记录一下

            #region 任务5: 用户设置一个M*N的二维数组,随机在 某处位置 设置为 1 。要求:使用代码识别出 1 所在的行和列,将 1 所在的行和列全部置 1

//用户设置行列
            Console.WriteLine("请输入行:");
            Console.Write("请输入列:");
            
            int hang = 0;
            int lie = 0;
            try
            {
                Console.SetCursorPosition(10, 0);
                hang = Convert.ToInt32(Console.ReadLine());

                Console.SetCursorPosition(10, 1);
                lie = Convert.ToInt32(Console.ReadLine());
            }
            catch (Exception)
            {
                Console.WriteLine("请输入合法数字!");
            }

  //随机放置1和0,尽量将1的数量减少          
            int[,] array = new int[hang, lie];
            int count = 0;//记录共有多少符合
            Random r = new Random();
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    array[i, j] = 0;
                    if (r.Next(0,10) == 1)
                    {
                        array[i, j] = 1;
                        count++;
                    }
                    Console.Write(array[i, j] + "\t ");
                }
                Console.WriteLine();
            }
            int[,] pos = new int[count, 2];//申明用来存放1位置的二维数组

            int x = 0;
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    if (array[i,j] == 1)
                    {
                        pos[x, 0] = i;
                        pos[x, 1] = j;
                        x++;
                    }
                }
            }

            Console.WriteLine("按任意键,开始置 1 !");
            Console.ReadKey();


            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int k = 0; k < array.GetLength(1); k++)
                {
                    for (int j = 0; j < pos.GetLength(0); j++)
                    {
                        if (i == pos[j, 0])
                        {
                            
                            array[i, k] = 1;
                        }
                        if (k == pos[j,1])
                        {
                            array[i, k] = 1;
                        }
                    }
                }
            }


            //将1的位置置红显示
            Console.ForegroundColor = ConsoleColor.White;
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    Console.ForegroundColor = array[i, j] == 1 ? ConsoleColor.Red : ConsoleColor.White;
                    Console.Write(array[i, j] + "\t ");
                }
                Console.WriteLine();
            }
            Console.ForegroundColor = ConsoleColor.White;
            #endregion

[展开全文]

在使用时可以用

结构体变量类型 变量名;

使用时可以用

变量名.结构体下方定义的参数;

如果这个参数也连接的一个结构体

则可以继续用

变量名.结构体下方定义的结构体的参数.参数所在结构体的参数。

 

[展开全文]

namespace 飞行棋
{
    enum E_Scene
    {
        StartGame,
        InGame,
        FinishGame
    }

    enum E_PlayerType
    {
        Player,
        PC
    }

    enum E_CellType
    {
        Normal,
        Bomb,
        PauseOneTurn,
        TimeTunnel,
    }
    #region Vecor2
    struct Vector2 //位置信息也是一个集合,可以直接写成结构
    {
        public int x;
        public int y;

        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    #endregion

    
    #region 格子构造体
    struct Cell
    {
        public E_CellType cellType;
        public Vector2 pos;//格子的位置

        //初始化构造函数
        public Cell(E_CellType cellType, int x, int y)
        {
            this.cellType = cellType;
            pos.x = x;
            pos.y = y;
        }

        public void DrawCell()
        {
            Console.SetCursorPosition(pos.x, pos.y);
            switch (cellType)
            {
                case E_CellType.Normal:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("□ ");
                    break;
                case E_CellType.PauseOneTurn:
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.Write("‖ ");
                    break;
                case E_CellType.Bomb:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("☢ ");
                    break;
                case E_CellType.TimeTunnel:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("¤ ");
                    break;
            }
        }
    }
    #endregion

    #region 棋盘
    struct Map
    {   //直接格子构造体作为数组
        public Cell[] A_Cells;

        public Map(int num , int x , int y)
        {
            A_Cells = new Cell[num];

            Random randomize = new Random();
            for (int i = 0; i < num; i++)
            {
                int probability = randomize.Next(1, 101);
                if (probability < 85 || i == 0 || i > num - 1)
                {
                    A_Cells[i].cellType = E_CellType.Normal; 
                }
                else if (probability < 90 )
                {
                    A_Cells[i].cellType = E_CellType.Bomb;
                }
                else if (probability < 95)
                {
                    A_Cells[i].cellType = E_CellType.PauseOneTurn;
                }
                else
                {
                    A_Cells[i].cellType = E_CellType.TimeTunnel;
                }
            }

            int cellPosX = x;
            int cellPosY = y;
            int cellCalcX = 0;
            int cellCalcY = 0;
            int mapPosXChange = 2;

            //横轴加10后纵轴加2,横轴减10后纵轴+2

            for (int i = 0; i < num; i++)
            {
                if (cellCalcX < 10)
                {
                    cellPosX += mapPosXChange;
                    cellCalcX ++;
                }
                else if (cellCalcY < 2)
                {
                    cellPosY++;
                    cellCalcY++;
                }
                else
                {
                    cellCalcX = 0;
                    cellCalcY = 0;
                    mapPosXChange = -mapPosXChange;
                }
                A_Cells[i].pos = new Vector2(cellPosX, cellPosY);
            }
        }

        public void DrawMap()
        {
            for (int i = 0; i < A_Cells.Length; i++)
            {
                A_Cells[i].DrawCell();
            }
        }
    }
    #endregion


    struct Player
    {
        public E_PlayerType playerType;
        public int nowWhere;
        public bool isPause;


        public Player(E_PlayerType player, int where)
        {
            playerType = player;
            nowWhere = where;
        }

        //不用写PlayerType和在哪里的原因是调用这个方法必然会调用结构所在方法
        public void DrawPlayer(Map cellInfo)
        {
            //将光标移动到所在格
            Cell cell;
            cell = cellInfo.A_Cells[nowWhere];
            Console.SetCursorPosition(cell.pos.x, cell.pos.y);

            if (playerType == E_PlayerType.Player)
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("✈ ");
            }
            if (playerType == E_PlayerType.PC)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("✈ ");
            }
        }
    }


    internal class Program 
    {
        #region 静态地图
        static void DrawMap()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            for (int row = 0; row < 31; row++)
            {
                Console.SetCursorPosition(0, row);
                if (row == 0 || row == 30 || row == 25 || row == 20)
                {
                    for (int column = 0; column < 52; column += 2)
                    {
                        Console.Write("■ ");
                    }
                }
                else
                {
                    Console.Write("■");
                    Console.SetCursorPosition(50, row);
                    Console.Write("■");
                }
            }
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2, 21);
            Console.Write("□:普通格子");
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.SetCursorPosition(2, 22);
            Console.Write("‖:暂停,一回合不动");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(28, 22);
            Console.Write("☢:炸弹,倒退5格");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.SetCursorPosition(2, 23);
            Console.Write("¤:时空隧道:随机倒退,暂停,换位置");
            Console.SetCursorPosition(2, 24);
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("✈:玩家");
            Console.SetCursorPosition(12, 24);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("✈:电脑");
            Console.SetCursorPosition(22, 24);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("◎:电脑与玩家重合");
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2, 26);
            Console.Write("按任意键开始扔骰子");
        }
        #endregion

        #region 画玩家
        static void DrawPlayer(Player player, Player pc, Map map)
        {
            //重合时
            if (player.nowWhere == pc.nowWhere)
            {
                //得到重合的位置
                Cell cell = map.A_Cells[player.nowWhere];
                Console.SetCursorPosition(cell.pos.x, cell.pos.y);
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write("◎");
            }
            //不重合的时候
            else
            {
                player.DrawPlayer(map);
                pc.DrawPlayer(map);
            }
        }
        #endregion

        #region 菜单
        static E_Scene Menu(ref E_Scene inScene)
        {
            bool inMenu = true;
            Console.Clear();

            if (inScene == E_Scene.StartGame)
            {
                Console.SetCursorPosition(23, 10);
                Console.Write("飞行棋");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(22, 13);
                Console.Write("开始游戏");
                Console.ForegroundColor = ConsoleColor.White;
            }
            else if (inScene == E_Scene.FinishGame)
            {
                Console.SetCursorPosition(22, 10);
                Console.Write("游戏结束");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(21, 13);
                Console.Write("回到主菜单");
                Console.ForegroundColor = ConsoleColor.White;
            }

            Console.SetCursorPosition(22, 15);
            Console.Write("退出游戏");

            byte row = 13;

            while (inMenu)
            {
                char input = Console.ReadKey(true).KeyChar;
                switch (input)
                {
                    #region W和S
                    case 'W':
                    case 'w':
                    case 'S':
                    case 's':
                        if (row == 13)
                        {
                            Console.SetCursorPosition(21, 13);
                            Console.Write("                 ");
                            switch (inScene)
                            {
                                case E_Scene.StartGame:
                                    Console.SetCursorPosition(22, 13);
                                    Console.Write("开始游戏");
                                    break;
                                case E_Scene.FinishGame:
                                    Console.SetCursorPosition(21, 13);
                                    Console.Write("回到主菜单");
                                    break;
                            }
                            Console.SetCursorPosition(22, 15);
                            Console.Write("                 ");
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.SetCursorPosition(22, 15);
                            Console.Write("退出游戏");
                            Console.ForegroundColor = ConsoleColor.White;
                            row = 15;
                        }
                        else if (row == 15)
                        {
                            Console.SetCursorPosition(22, 15);
                            Console.Write("退出游戏");
                            Console.SetCursorPosition(21, 13);
                            Console.Write("                 ");
                            Console.ForegroundColor = ConsoleColor.Red;
                            switch (inScene)
                            {
                                case E_Scene.StartGame:
                                    Console.SetCursorPosition(22, 13);
                                    Console.Write("开始游戏");
                                    break;
                                case E_Scene.FinishGame:
                                    Console.SetCursorPosition(21, 13);
                                    Console.Write("回到主菜单");
                                    break;
                            }
                            Console.ForegroundColor = ConsoleColor.White;
                            row = 13;
                        }
                        break;
                    #endregion

                    case 'J':
                    case 'j':
                        if(row == 13 && inScene == E_Scene.StartGame)
                        {
                            inScene = E_Scene.InGame;
                            inMenu = false;
                        }
                        else if(row == 15)
                        {
                            Console.Clear();
                            inMenu = false;
                            Environment.Exit(0);
                        }
                        else if(inScene == E_Scene.FinishGame)
                        {
                            Console.Clear();
                            inScene = E_Scene.StartGame;
                            inMenu = false;
                            Menu(ref inScene);
                        }
                        break;
                }
            }

            return inScene;
        }
        #endregion

        #region 移动
        static void Movement(ref Player player, ref Player other, Map map, int dice, ref bool gameOver)
        {
            Cell cell;
            if (!player.isPause)
            {
                if (player.nowWhere < map.A_Cells.Length - 1)
                {
                    cell.cellType = map.A_Cells[player.nowWhere += dice].cellType;
                    switch (cell.cellType)
                    {
                        case E_CellType.Normal:
                            player.nowWhere += dice;
                            break;
                        case E_CellType.Bomb:
                            player.nowWhere += dice - 5;
                            break;
                        case E_CellType.PauseOneTurn:
                            player.nowWhere += dice;
                            player.isPause = true;
                            break;
                        case E_CellType.TimeTunnel:
                            Random r = new Random();
                            int result = r.Next(1, 4);
                            switch (result)
                            {
                                case 1:
                                    player.nowWhere += dice - 5;
                                    break;
                                case 2:
                                    player.nowWhere += dice;
                                    player.isPause = true;
                                    break;
                                case 3:
                                    Player check = new Player();
                                    check.nowWhere = player.nowWhere;
                                    player.nowWhere = other.nowWhere;
                                    other.nowWhere = check.nowWhere;
                                    break;
                            }
                            break;
                    }
                }

            }
            else
            {
                player.isPause = false;
            }
            if(player.nowWhere < 0)
            {
                player.nowWhere = 0;
            }

            if (player.nowWhere >= map.A_Cells.Length - 1)
            {
                player.nowWhere = map.A_Cells.Length - 1;
                gameOver = true;
                Console.Clear();
                E_Scene inScene = E_Scene.FinishGame;
                Menu(ref inScene);
            }
        }
        #endregion


        #region 总命令
        static void GameMain(ref Player player ,ref Player pc,  Map map, int dice, ref int turn, ref bool gameOver)
        {

            if (turn % 2 == 0 )
            {
                Movement(ref pc, ref player, map, dice, ref gameOver);
                map.DrawMap();
                DrawPlayer(player, pc, map);
            }
            else
            {
                Movement(ref player, ref pc, map, dice ,ref gameOver);
                map.DrawMap();
                DrawPlayer(player, pc, map);
            }

            turn++;
        }
        #endregion


        static void Main(string[] args)
        {
            Console.SetWindowSize(52, 31);
            Console.SetBufferSize(52, 31);
            Console.CursorVisible = false;

            bool inGame = true;
            E_Scene inScene = E_Scene.StartGame;

            while (inGame)
            {
                switch (inScene)
                {
                    case E_Scene.StartGame:
                        Menu(ref inScene);
                        break;
                    case E_Scene.InGame:
                        Console.Clear();
                        DrawMap();
                        Map checBoard = new Map(100, 14, 3);
                        checBoard.DrawMap();
                        Player player = new Player(E_PlayerType.Player, 0);
                        Player pc = new Player(E_PlayerType.PC, 0);
                        DrawPlayer(player, pc, checBoard);
                        Random r = new Random();
                        int turn = 1;
                        bool gameover = false;
                        while (!gameover)
                        {
                            Console.ReadKey(true);
                            int dice = r.Next(1, 7);

                            GameMain(ref player, ref pc, checBoard, dice, ref turn ,ref gameover);
                        }
                        break;
                    case E_Scene.FinishGame:
                        Menu(ref inScene);
                        break;
                }
            }
        }
    }
}

[展开全文]

 

    internal class Program
    {
        static int[] Sort(int[] array,string howto)
        {
            if (howto == "ascending")
            {
                for (int i = 0; i < array.Length; i++)
                {
                    int index = 0;

                    for (int j = 1; j < array.Length - i; j++)
                    {
                        if (array[index] < array[j])
                        {
                            index = j;
                        }
                    }

                    if (index != array.Length - 1 - i)
                    {
                        int temp = array[index];
                        array[index] = array[array.Length - 1 - i];
                        array[array.Length - 1 - i] = temp;
                    }
                }
            }
            else if (howto == "descending")
            {
                for (int i = 0; i < array.Length; i++)
                {
                    int index = 0;

                    for (int j = 1; j < array.Length - i; j++)
                    {
                        if (array[index] > array[j])
                        {
                            index = j;
                        }
                    }

                    if (index != array.Length - 1 - i)
                    {
                        int temp = array[index];
                        array[index] = array[array.Length - 1 - i];
                        array[array.Length - 1 - i] = temp;
                    }
                }

            }
            return array;
        }
        static void Main(string[] args)
        {
            Random randomaize = new Random();

            int[] integer = new int[20];

            for (int i = 0; i < integer.Length; i++)
            {
                integer[i] = randomaize.Next(101);
                Console.Write(integer[i] + " ");
            }

            int[] ascending = Sort(integer, "ascending");
            Console.WriteLine("");
            for (int i = 0; i < ascending.Length; i++)
            {
                Console.Write(ascending[i] + " ");
            }

            int[] descending = Sort(integer, "descending");
            Console.WriteLine("");
            for (int i = 0; i < descending.Length; i++)
            {
                Console.Write(descending[i] + " ");
            }
        }
    }

[展开全文]

两题合在一起做,记住方法前面加static静态,并且注意数组的存储是地址,赋值的时候如果后面修改过具体数值,前面也会变

    internal class Program
    {
        static int[] Sort( int[] nums, string howto) 
        {
            bool isSort = false;
            if (howto == "ascending")
            {
                for (int i = 0; i < nums.Length; i++)
                {
                    isSort = false;
                    for (int j = 0; j < nums.Length - 1 - i; j++)
                    {
                        if (nums[j] > nums[j + 1])
                        {
                            isSort = true;
                            int middle = nums[j];
                            nums[j] = nums[j + 1];
                            nums[j + 1] = middle;
                        }
                    }
                    if (!isSort)
                    {
                        break;
                    }
                }
            }
            else if(howto == "descending") 
            {
                for (int i = 0; i < nums.Length; i++)
                {
                    isSort = false;
                    for (int j = 0; j < nums.Length - 1 - i; j++)
                    {
                        if (nums[j] < nums[j + 1])
                        {
                            isSort = true;
                            int middle = nums[j];
                            nums[j] = nums[j + 1];
                            nums[j + 1] = middle;
                        }
                    }
                    if (!isSort)
                    {
                        break;
                    }
                }
            }

            return nums;
        }

        static void Main(string[] args)
        {
            Random randomaize = new Random();

            int[] integer = new int[20];

            for (int i = 0; i < integer.Length; i++)
            {
                integer[i] = randomaize.Next(101);
                Console.Write(integer[i] + " ");
            }


            int[] ascending = Sort(integer, "ascending");
            Console.WriteLine("");
            for (int i = 0; i < ascending.Length; i++)
            {
                Console.Write(ascending[i] + " ");
            }

            int[] descending = Sort(integer, "descending");
            Console.WriteLine("");
            for (int i = 0; i < descending.Length; i++)
            {
                Console.Write(descending[i] + " ");
            }

        }
    }

[展开全文]

数组声明里存储的东西可以是结构体的构造方法

DoSomething[] array[i] = new DoSomething[10]

方法的参数也可以声明为结构体的使用,而且同样适用于ref和out

void DoSomething(ref DoAnything anything)

{

}

 

      public Monsters(string name)
        {
            this.name = name;
            Random randomize = new Random();
            this.atk = randomize.Next(5, 20);
        }

        public void StorgeMonsters()
        {
            Monsters[] monster = new Monsters[10];
            for (int i = 0; i < monster.Length; i++)
            {
                monster[i] = new Monsters("小怪兽" + i);
                monster[i].Atk();
            }
        }

        public void Atk()
        {
            Console.WriteLine("{0}的攻击力为{1}", name, atk);
        }

    struct OutMan
    {
        public string name;
        public int atk;
        public int hp;
        public int def;

        public OutMan(string name, int atk, int hp)
        {
            this.name = name;
            this.atk = atk;
            this.hp = hp;
        }

        public void Atk(ref LittleMonsters boss)
        {
            boss.hp = this.atk - boss.def;
            Console.WriteLine("{0}攻击了{1}造成{2}点伤害,{1}剩余血量为{3}", this.name, boss.name, (this.atk - boss.def), boss.hp);
        }
 
    }

    struct LittleMonsters
    {
        public string name;
        public int atk;
        public int hp;
        public int def;

        public LittleMonsters(string name, int atk, int hp)
        {
            this.name = name;
            this.atk = atk;
            this.hp = hp;
        }
        public void Atk(ref OutMan outman)
        {
            outman.hp = this.atk - outman.def;
            Console.WriteLine("{0}攻击了{1}造成{2}点伤害,{1}剩余血量为{3}", this.name, outman.name, (this.atk - outman.def), outman.hp);
        }

    }

[展开全文]

namespace 结构体
{
    struct Students
    {
        public string name;
        public bool sex;
        public int age;
        public string classAndGrade;
        public string professional;

        public Students(string name, bool sex, int age, string classAndGrade,string professional)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.classAndGrade = classAndGrade;
            this.professional = professional;
        }


        public void EssentialInformation()
        {
            Console.WriteLine("姓名:{0} 性别:{1} 年龄:{2} 班级:{3} 专业:{4}", name, (sex ? "Female" : "Male"), age, classAndGrade, professional);
        }
    }

    struct Rectangle
    {
        public float length;
        public float width;

        public Rectangle(float length,float width)
        {
            this.length = length;
            this.width = width;
        }

        public void CalcRectangle()
        {
            float area = length * width;
            float peri = 2 * (length + width);
            Console.WriteLine("矩形的周长为{0},面积为{1}", peri, area);
        }
    }

    struct PlayerInfo
    {
        public string name;
        public string job;
        
        public PlayerInfo(string name, string job)
        {
            this.name = name;
            this.job = job;
        }

        public void Input()
        {
            Console.WriteLine("请输入姓名:");
            string name = Console.ReadLine();
            Console.WriteLine("请选择职业:1.Warrior,2.Hunter,3.Witcher");
            int i = int.Parse(Console.ReadLine());
            string job = null;

            switch (i)
            {
                case 1:
                    job = "Warrior";
                    break;
                case 2:
                    job = "Hunter";
                    break;
                case 3:
                    job = "Witcher";
                    break;
            }

            PlayerInfo playerinfo = new PlayerInfo(name, job);
            playerinfo.PlayerAttack();
        }

        public void PlayerAttack()
        {
            string skill = null;
            switch (job)
            {
                case "Warrior":
                    skill = "Charge";
                    break;
                case "Hunter":
                    skill = "Fake Death";
                    break;
                case "Witcher":
                    skill = "Arcane Shock";
                    break;
            }

            Console.WriteLine("{0}{1}释放了{2}", job, name, skill);
        }
    }

    struct Monsters
    {
        string name;
        int atk;
        int hp;

        public Monsters(string name,int atk )
        {
            this.name = name;
            this.atk = atk;
        }

        public void StorgeMonsters()
        {
            Monsters monster = new Monsters(null, 0);
            int[,] monsters = new int[10, 2];
            for (int i = 0; i < monsters.GetLength(0); i++)
            {
                for (int j = 0; j < monsters.GetLength(1); j++)
                {
                    if (j == 1)
                    {
                        monsters[i,j] = 20;
                        monster.atk = monsters[i, j];
                        monster.name = Convert.ToString(i);
                        Console.WriteLine("小怪兽{0},攻击力为{1}", monster.name, monster.atk);
                    }
                }
            }
        }
    }

    struct OutMan
    {
        public string name;
        public int atk;
        public int hp;

        public OutMan(string name, int atk, int hp)
        {
            this.name = name;
            this.atk = atk;
            this.hp = hp;
        }

 
    }

    struct LittleMonsters
    {
        public string name;
        public int atk;
        public int hp;

        public LittleMonsters(string name, int atk, int hp)
        {
            this.name = name;
            this.atk = atk;
            this.hp = hp;
        }
    }

    internal class Program
    {
        public int OutManBeatLittleMonsters()
        {
            OutMan outman = new OutMan();
            LittleMonsters littlemonsters = new LittleMonsters();

            littlemonsters.hp -= outman.atk;

            return littlemonsters.hp;
        }

        public int MonstersBeatOutMAn()
        {
            OutMan outman = new OutMan();
            LittleMonsters littlemonsters = new LittleMonsters();

            outman.hp -= littlemonsters.atk;

            return outman.hp;
        }
        static void Main(string[] args)
        {
            Students jackson = new("Jackson", false, 18, "1 Class 1 Grade", "Math");
            Students nelson = new("Nelson", true, 19, "4 Class 2 Grade", "Philosophy:");

            jackson.EssentialInformation();
            nelson.EssentialInformation();

            Rectangle rectangle = new Rectangle(3.7f, 8.2f);
            rectangle.CalcRectangle();

            PlayerInfo you = new PlayerInfo(null, null);
            you.Input();

            Monsters setMonsters = new Monsters(null, 0);
            setMonsters.StorgeMonsters();
        }
    }
}

[展开全文]

撞坑总结:

除了void外其他所有递归方法的任何判定条件(if else switch)的里面和外面都得返回一个合理的值,不然会报错“不是所有的输出都能返回一个具体的值”

 

输入一个数字后在return中依次调用方法往下减,减到等于1的时候让他直接返回这个方法(函数)为1的值,之后整个程序自动回退,此为递归

 

在递归第一次return的时候,这个return并没有 真正的执行,而是撞到了里面规定的方法(函数),继续循环,而当他撞到if 里面没有再让他进方法的return后,才会真实的后撤,并且return

        static int Factorial(int num)
        {
            if (num == 1)
            {
                return Factorial(1);
            }
            return num * Factorial(num - 1);
        }


        static void FactorialSum( int i = 1, long sum = 0)
        {
            if (i > 10)
            {
                Console.WriteLine("累乘再累加后的值为:" + sum);
                return;
            }
            sum += Factorial(i);
            i++;

            FactorialSum( i, sum);
        }

 

        static int FactorialSum(int num = 10)
        {

            if (num == 1)
            {
                return Factorial(1);
            }
            return Factorial(num) + FactorialSum(num - 1);
        }

 

短路不止可以用在逻辑判断的if switch里,也可以用来判断方法是否执行

 

比如

 

return true || 方法();【注意这个方法返回值需要是布尔】

 

只要满足条件方法就不会执行了

 

[展开全文]

授课教师

游戏开发前端主程

课程特色

视频(42)
下载资料(37)

学员动态

从零开始学unity 加入学习
DCMax 加入学习
鱼排fish 加入学习
星星屑屑诶嘿嘿 加入学习
haixiao_stack 加入学习