Advertisement
illiden

DynamicArray

Jul 1st, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Store
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<int> array = new List<int>();
  11.             bool work = true;
  12.  
  13.             while(work)
  14.             {
  15.                 Console.WriteLine("Доступные команды: \n\n" +
  16.                             "1. Создать новый массив чисел \n\n" +
  17.                             "2. Добавить числа к массиву \n\n" +
  18.                             "3. Отсортировать числа \n\n" +
  19.                             "4. Вывести сумму элементов массива \n\n" +
  20.                             "5. Вывести элементы массива \n\n" +
  21.                             "6. Выход\n");
  22.  
  23.                 switch(Console.ReadLine())
  24.                 {
  25.                     case "1":
  26.                         array = new List<int>();
  27.                         FillArrayMode(array);
  28.                         break;
  29.                     case "2":
  30.                         FillArrayMode(array);
  31.                         break;
  32.                     case "3":
  33.                         array.Sort();
  34.                         break;
  35.                     case "4":
  36.                         int sum = 0;
  37.                         foreach (var number in array)
  38.                         {
  39.                             sum += number;
  40.                         }
  41.                         Console.WriteLine("Сумма всех чисел равна " + sum);
  42.                         Console.ReadKey();
  43.                         break;
  44.                     case "5":
  45.                         foreach (var number in array)
  46.                         {
  47.                             Console.Write(number + " ");
  48.                         }
  49.                         Console.ReadKey();
  50.                         break;
  51.                     case "6":
  52.                         work = false;
  53.                         break;
  54.                 }
  55.                 Console.Clear();
  56.             }
  57.         }
  58.  
  59.         static void FillArrayMode(List<int> array)
  60.         {
  61.             Console.Clear();
  62.             Console.WriteLine("Вы в режиме заполения массива. Чтобы добавить число, введите его и нажмите enter. Чтобы выйти введите esc");
  63.             while (true)
  64.             {
  65.                 string input = Console.ReadLine();
  66.                 if (input == "esc")
  67.                 {
  68.                     break;
  69.                 }
  70.                 if (Int32.TryParse(input, out int number))
  71.                 {
  72.                     array.Add(number);
  73.                 }
  74.                 else
  75.                 {
  76.                     Console.WriteLine("Введите число!!!");
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement