Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const string CommandExit = "exit";
- const string CommandSum = "sum";
- const string CommandNew = "new";
- int[] array = { };
- bool isInfiniteRunning = true;
- string userInput;
- Console.WriteLine("Здравствуйте! Введите число для добавления в массив или команды sum, exit или new!");
- while (isInfiniteRunning)
- {
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandExit:
- isInfiniteRunning = false;
- Console.WriteLine("До свидания!");
- break;
- case CommandSum:
- int sum = 0;
- Console.WriteLine("Получился массив:");
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write(array[i] + " ");
- sum += array[i];
- }
- Console.WriteLine();
- Console.WriteLine($"Сумма всех элементов массива: {sum}");
- break;
- case CommandNew:
- array = new int[0];
- Console.WriteLine($"Массив очищен.");
- break;
- default:
- int newElement = Convert.ToInt32(userInput);
- int[] temporaryArray = new int[array.Length + 1];
- for (int i = 0; i < array.Length; i++)
- {
- temporaryArray[i] = array[i];
- }
- temporaryArray[temporaryArray.Length - 1] = newElement;
- array = temporaryArray;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement