Меню с выбором программы - C#

Узнай цену своей работы

Формулировка задачи:

есть две программы, нужно добавить меню с возможностью выбора методов через arrow key с выделением текста первая:
Листинг программы
  1. Задан числовой массив, состоящий из n элементов (n<=100).
  2. Написать метод, определяющий, есть ли среди элементов с нечетными номерами элементы, кратные 17.
  3. */
  4. class Program
  5. {
  6. static void input_arr(out int[,] arr)
  7. {
  8. Console.Write("количество строк:");
  9. int n = int.Parse(Console.ReadLine());
  10. Console.Write("количество столбцов:");
  11. int m = int.Parse(Console.ReadLine());
  12. arr = new int[n, m];
  13. Random rand = new Random();
  14. for (int i = 0; i < arr.GetLength(0); i++)
  15. {
  16. for (int j = 0; j < arr.GetLength(1); j++)
  17. {
  18. arr[i, j] = rand.Next(0, 26);
  19. }
  20. }
  21. }
  22. static void output_arr (int [,]arr)
  23. {
  24. for (int i=0;i<arr.GetLength(0);i++)
  25. {
  26. for (int j = 0; j < arr.GetLength(1); j++)
  27. {
  28. Console.Write("{0} ",arr[i,j]);
  29. }
  30. Console.WriteLine();
  31. }
  32. }
  33. //int[] Meth(int[] arr)
  34. //{
  35. // List<int> res = new List<int>();
  36. // for (int i = 0; i < arr.Length; i++)
  37. // {
  38. // if (arr[i] % 17 == 0 && i % 2 != 0) { res.Add(arr[i]); }
  39. // }
  40. // return res.ToArray();
  41. //}
  42. static int[] Meth(int[,] arr)
  43. {
  44. List<int> res = new List<int>();
  45. for (int i = 0; i < arr.GetLength(0); i++)
  46. {
  47. for (int j = 0; j < arr.GetLength(1); j++)
  48. {
  49. if (arr[i, j] % 17== 0 && (i + j) % 2 != 0)
  50. {
  51. res.Add(arr[i, j]);
  52. }
  53. }
  54. }
  55. return res.ToArray();
  56. }
  57. static void Main(string[] args)
  58. {
  59. int[,] array;
  60. //int[] array;
  61. input_arr(out array);
  62. output_arr(array);
  63. int [] elem = Meth(array);
  64. foreach (int item in elem)
  65. Console.Write("{0} ", item);
  66. }
  67. }
вторая:
Листинг программы
  1. Задан числовой массив, состоящий из n элементов (n<=100).
  2. Используя сортировку массива, определить номер последнего чётного элемента массива.
  3. */
  4. class Program
  5. {
  6. static void input_arr (out int []arr)
  7. {
  8. Console.Write("количество элементов:");
  9. int n = int.Parse(Console.ReadLine());
  10. arr = new int[n];
  11. Random rand = new Random();
  12. for (int i=0;i<arr.Length;i++)
  13. {
  14. arr[i] = rand.Next(-25, 26);
  15. }
  16. }
  17. static void output_arr(int[] arr)
  18. {
  19. for (int i = 0; i < arr.Length; i++)
  20. {
  21. Console.Write("{0} ", arr[i]);
  22. }
  23. }
  24. static void sort_insert(ref int[] arr)
  25. {
  26. int tmp, i, j;
  27. for (i = 0; i < arr.Length; i++)
  28. {
  29. tmp = arr[i];
  30. for (j = i - 1; j >= 0 && arr[j] > tmp; j--)
  31. {
  32. arr[j + 1] = arr[j];
  33. }
  34. arr[j + 1] = tmp;
  35. }
  36. }
  37. static int meth(int[] arr)
  38. {
  39. int number = 0;
  40. for (int i = arr.Length - 1; i > 0; i--)
  41. if (arr[i] % 2 == 0)
  42. { number = i;
  43. break;
  44. }
  45. return number;
  46. }
  47. static void Main(string[] args)
  48. {
  49. int[] array;
  50. input_arr(out array);
  51. Console.WriteLine("до сортировки");
  52. output_arr(array);
  53. sort_insert(ref array);
  54. Console.WriteLine("\nпосле сортировки");
  55. output_arr(array);
  56. Console.WriteLine();
  57. int index = meth(array);
  58. Console.WriteLine("номер последнего четного элемента:{0}", index);
  59. }
  60. }
да ссылка на тему с тем же вопросом тоже вариант

Решение задачи: «Меню с выбором программы»

textual
Листинг программы
  1.     class Program
  2.     {
  3.         delegate void method();
  4.         static void Main(string[] args)
  5.         {
  6.  
  7.             string[] items = { "Действие 1", "Действие 2", "Действие 3", "Выход" };
  8.             method[] methods = new method[] { Method1, Method2, Method3, Exit };
  9.             ConsoleMenu menu = new ConsoleMenu(items);
  10.             int menuResult;
  11.             do
  12.             {
  13.                 menuResult = menu.PrintMenu();
  14.                 methods[menuResult]();
  15.                 Console.WriteLine("Для продолжения нажмите любую клавишу");
  16.                 Console.ReadKey();
  17.             } while (menuResult != items.Length - 1);
  18.         }
  19.  
  20.         static void Method1()
  21.         {
  22.             Console.WriteLine("Выбрано действие 1");
  23.         }
  24.         static void Method2()
  25.         {
  26.             Console.WriteLine("Выбрано действие 2");
  27.         }
  28.         static void Method3()
  29.         {
  30.             Console.WriteLine("Выбрано действие 3");
  31.         }
  32.         static void Exit()
  33.         {
  34.             Console.WriteLine("Приложение заканчивает работу!");
  35.         }
  36.     }
  37.  
  38.  
  39.     class ConsoleMenu
  40.     {
  41.         string[] menuItems;
  42.         int counter = 0;
  43.         public ConsoleMenu(string[] menuItems)
  44.         {
  45.             this.menuItems = menuItems;
  46.         }
  47.  
  48.         public int PrintMenu()
  49.         {
  50.             ConsoleKeyInfo key;
  51.             do
  52.             {
  53.                 Console.Clear();
  54.                 for (int i = 0; i < menuItems.Length; i++)
  55.                 {
  56.                     if (counter == i)
  57.                     {
  58.                         Console.BackgroundColor = ConsoleColor.Cyan;
  59.                         Console.ForegroundColor = ConsoleColor.Black;
  60.                         Console.WriteLine(menuItems[i]);
  61.                         Console.BackgroundColor = ConsoleColor.Black;
  62.                         Console.ForegroundColor = ConsoleColor.White;
  63.                     }
  64.                     else
  65.                         Console.WriteLine(menuItems[i]);
  66.  
  67.                 }
  68.                 key = Console.ReadKey();
  69.                 if (key.Key == ConsoleKey.UpArrow)
  70.                 {
  71.                     counter--;
  72.                     if (counter == -1) counter = menuItems.Length - 1;
  73.                 }
  74.                 if (key.Key == ConsoleKey.DownArrow)
  75.                 {
  76.                     counter++;
  77.                     if (counter == menuItems.Length) counter = 0;
  78.                 }
  79.             }
  80.             while (key.Key != ConsoleKey.Enter);
  81.             return counter;
  82.         }
  83.  
  84.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.083 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы