using System.Diagnostics;int[,] grid = { {1, 2, 3,}, {4, 5, 6,}, {7, 8, 9,},};int kolumnMAX = grid.GetLength(0) -1 ;int radMAX = grid.GetLength(1) -1;int kolumn = 0;int rad = 0;ConsoleKeyInfo key; Console.Clear();for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { Console.Write(grid[i,j]); Console.Write(" "); } Console.WriteLine(" "); } while (true){ key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.UpArrow: kolumn--; break; case ConsoleKey.DownArrow: kolumn++; break; case ConsoleKey.LeftArrow: rad--; break; case ConsoleKey.RightArrow: rad++; break; } if (kolumn>kolumnMAX) { kolumn=0; } else if(kolumn<0) { kolumn = kolumnMAX; } if (rad>radMAX) { rad=0; } else if(rad<0) { rad = radMAX; } //Switch gillar ej dynamiska tal /* switch (kolumn) { case >2: kolumn = 0; break; case <0: kolumn = 2; break; } */ /* switch (rad) { case >2: rad = 0; break; case <0: rad = 2; break; } */ Console.SetCursorPosition(rad , kolumn); }
int[,] grid = { {1, 2, 3,}, {4, 5, 6,}, {7, 8, 9,},};int kolumn = 0;int rad = 0;ConsoleKeyInfo key; Console.Clear();for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { Console.Write(grid[i,j]); Console.Write(" "); } Console.WriteLine(" "); } while (true){ key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.UpArrow: kolumn++; break; case ConsoleKey.DownArrow: kolumn--; break; case ConsoleKey.LeftArrow: rad--; break; case ConsoleKey.RightArrow: rad++; break; } switch (kolumn) { case >2: kolumn = 0; break; case <1: kolumn = 2; break; } switch (rad) { case >2: rad = 0; break; case <1: rad = 2; break; } Console.SetCursorPosition(kolumn , rad); }