二維數組需要座標值時可以這麼寫

class Program {
        static void Main(string[] args) {
            int row = 10;
            int col = 8;
            // 因爲x是橫向座標,所以代表的應該是列,y則代表行,而寫左邊時習慣爲x,y 所以定義二維數組時應該爲[列,行]
            int[,] map = new int[col, row];

            int x = 2, y = 3;
            map[x, y] = 6;

            // 遍歷行
            for (int j = 0; j < map.GetLength(1); j++) {
                // 遍歷列各字符,連接成行
                for (int i = 0; i < map.GetLength(0); i++) {
                    Console.Write(map[i,j].ToString().PadRight(4));
                }
                // 換行
                Console.WriteLine();
            }
        }
    }
發佈了26 篇原創文章 · 獲贊 7 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章