【C#】 迴圈小練習_迴型矩陣

  • 1195
  • 0
  • C#
  • 2020-03-06

迴圈練習:列印出行迴型矩陣

利用二維陣列,將數字按照最近很紅的【順時中】順時鐘..寫入陣列裡面
再利用雙迴圈輸出陣列內容。
 

class Program
    {
        //建立一些靜態的類別
        public static int num; //矩陣大小
        public static int value = 1; //起始數字
        public static int[,] arr; //二為陣列
        public static Direction lastDirection = Direction.Right; //方向

        //設定移動的方向
        public enum Direction
        {
            Right, Down, Left, Up
        }

        public static void Main(string[] args)
        {
            //迴圈矩陣

            Console.Write("請輸入一個字元:");
            num = Convert.ToInt32(Console.ReadLine());//設定正方形的大小
            Console.WriteLine("輸入碼為:{0}", num);
            
            arr = new int[num, num];
            //int c = snake.Length;
            NumArray();
            print(arr);
            Console.ReadLine();
        }

        //
        public static void NumArray()
        {
            int row = 0,  col = 0;
            arr.SetValue(value, 0, 0);
            for (int c = 0; c < num * num; c++)
            {
                arr.SetValue(value, row, col);
                lastDirection = findDirection(row, col);
                switch (lastDirection)
                {
                    case Direction.Right:
                        {
                            col++;
                            break;
                        }
                    case Direction.Down:
                        {
                            row++;
                            break;
                        }
                    case Direction.Left:
                        {
                            col--;
                            break;
                        }
                    case Direction.Up:
                        {
                            row--;
                            break;
                        }
                    default:
                        Console.WriteLine("系統錯誤,超出界線");
                        //Console.Read();
                        break;
                }
                value++;
            }

        }

        public static Direction findDirection(int row, int col)
        {
            Direction direction = lastDirection;
            switch (direction)
            {
                case Direction.Right:
                    {
                        if ((col == num - 1) || (arr[row, col + 1] != 0))
                        {
                            direction = Direction.Down;
                        }
                        break;
                    }
                case Direction.Down:
                    {
                        if ((row == num - 1) || (arr[row + 1, col] != 0))
                        {
                            direction = Direction.Left;
                        }
                        break;
                    }
                case Direction.Left:
                    {
                        if ((col == 0) || (arr[row, col - 1] != 0))
                        {
                            direction = Direction.Up;
                        }
                        break;
                    }
                case Direction.Up:
                    {
                        if (arr[row - 1, col] != 0)
                        {
                            direction = Direction.Right;
                        }
                        break;
                    }
            }

            return direction;
        }

        static void print(int[,] arr)
        {
            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    Console.Write("{0} ", arr.GetValue(i, j).ToString().PadLeft(2,'0'));
                }
                Console.WriteLine();
            }
        }
    }

輸出結果

水滴可成涓流,涓流可成湖泊大海。
汲取累積知識,將知識堆積成常識;將常識探究成學識;將學識簡化為知識;授人自省。