Меню с выбором программы - C#
Формулировка задачи:
есть две программы, нужно добавить меню с возможностью выбора методов через arrow key с выделением текста
первая:
вторая:
Листинг программы
- Задан числовой массив, состоящий из n элементов (n<=100).
- Написать метод, определяющий, есть ли среди элементов с нечетными номерами элементы, кратные 17.
- */
- class Program
- {
- static void input_arr(out int[,] arr)
- {
- Console.Write("количество строк:");
- int n = int.Parse(Console.ReadLine());
- Console.Write("количество столбцов:");
- int m = int.Parse(Console.ReadLine());
- arr = new int[n, m];
- Random rand = new Random();
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- arr[i, j] = rand.Next(0, 26);
- }
- }
- }
- static void output_arr (int [,]arr)
- {
- for (int i=0;i<arr.GetLength(0);i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- Console.Write("{0} ",arr[i,j]);
- }
- Console.WriteLine();
- }
- }
- //int[] Meth(int[] arr)
- //{
- // List<int> res = new List<int>();
- // for (int i = 0; i < arr.Length; i++)
- // {
- // if (arr[i] % 17 == 0 && i % 2 != 0) { res.Add(arr[i]); }
- // }
- // return res.ToArray();
- //}
- static int[] Meth(int[,] arr)
- {
- List<int> res = new List<int>();
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- if (arr[i, j] % 17== 0 && (i + j) % 2 != 0)
- {
- res.Add(arr[i, j]);
- }
- }
- }
- return res.ToArray();
- }
- static void Main(string[] args)
- {
- int[,] array;
- //int[] array;
- input_arr(out array);
- output_arr(array);
- int [] elem = Meth(array);
- foreach (int item in elem)
- Console.Write("{0} ", item);
- }
- }
Листинг программы
- Задан числовой массив, состоящий из n элементов (n<=100).
- Используя сортировку массива, определить номер последнего чётного элемента массива.
- */
- class Program
- {
- static void input_arr (out int []arr)
- {
- Console.Write("количество элементов:");
- int n = int.Parse(Console.ReadLine());
- arr = new int[n];
- Random rand = new Random();
- for (int i=0;i<arr.Length;i++)
- {
- arr[i] = rand.Next(-25, 26);
- }
- }
- static void output_arr(int[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- {
- Console.Write("{0} ", arr[i]);
- }
- }
- static void sort_insert(ref int[] arr)
- {
- int tmp, i, j;
- for (i = 0; i < arr.Length; i++)
- {
- tmp = arr[i];
- for (j = i - 1; j >= 0 && arr[j] > tmp; j--)
- {
- arr[j + 1] = arr[j];
- }
- arr[j + 1] = tmp;
- }
- }
- static int meth(int[] arr)
- {
- int number = 0;
- for (int i = arr.Length - 1; i > 0; i--)
- if (arr[i] % 2 == 0)
- { number = i;
- break;
- }
- return number;
- }
- static void Main(string[] args)
- {
- int[] array;
- input_arr(out array);
- Console.WriteLine("до сортировки");
- output_arr(array);
- sort_insert(ref array);
- Console.WriteLine("\nпосле сортировки");
- output_arr(array);
- Console.WriteLine();
- int index = meth(array);
- Console.WriteLine("номер последнего четного элемента:{0}", index);
- }
- }
да ссылка на тему с тем же вопросом тоже вариант
Решение задачи: «Меню с выбором программы»
textual
Листинг программы
- class Program
- {
- delegate void method();
- static void Main(string[] args)
- {
- string[] items = { "Действие 1", "Действие 2", "Действие 3", "Выход" };
- method[] methods = new method[] { Method1, Method2, Method3, Exit };
- ConsoleMenu menu = new ConsoleMenu(items);
- int menuResult;
- do
- {
- menuResult = menu.PrintMenu();
- methods[menuResult]();
- Console.WriteLine("Для продолжения нажмите любую клавишу");
- Console.ReadKey();
- } while (menuResult != items.Length - 1);
- }
- static void Method1()
- {
- Console.WriteLine("Выбрано действие 1");
- }
- static void Method2()
- {
- Console.WriteLine("Выбрано действие 2");
- }
- static void Method3()
- {
- Console.WriteLine("Выбрано действие 3");
- }
- static void Exit()
- {
- Console.WriteLine("Приложение заканчивает работу!");
- }
- }
- class ConsoleMenu
- {
- string[] menuItems;
- int counter = 0;
- public ConsoleMenu(string[] menuItems)
- {
- this.menuItems = menuItems;
- }
- public int PrintMenu()
- {
- ConsoleKeyInfo key;
- do
- {
- Console.Clear();
- for (int i = 0; i < menuItems.Length; i++)
- {
- if (counter == i)
- {
- Console.BackgroundColor = ConsoleColor.Cyan;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.WriteLine(menuItems[i]);
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.White;
- }
- else
- Console.WriteLine(menuItems[i]);
- }
- key = Console.ReadKey();
- if (key.Key == ConsoleKey.UpArrow)
- {
- counter--;
- if (counter == -1) counter = menuItems.Length - 1;
- }
- if (key.Key == ConsoleKey.DownArrow)
- {
- counter++;
- if (counter == menuItems.Length) counter = 0;
- }
- }
- while (key.Key != ConsoleKey.Enter);
- return counter;
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д