Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- internal class Program
- {
- static void Main(string[] args)
- {
- const string CommandExit = "exit";
- const string CommandSum = "sum";
- int[] numbers = new int[0];
- int[] tempNumbers;
- int sum = 0;
- string userInput;
- bool inProgram = true;
- while (inProgram)
- {
- Console.Clear();
- foreach (int number in numbers)
- {
- Console.Write(number + " ");
- }
- Console.Write($"\n{CommandSum} - Вывести сумму чисел массива.\n" +
- $"{CommandExit} - Завершить программу.\nВведите любое число, что бы добавить его в массив.\nВвод: ");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandSum:
- foreach (int number in numbers)
- {
- sum += number;
- }
- Console.WriteLine("Сумма всех чисел = " + sum);
- sum = 0;
- break;
- case CommandExit:
- Console.WriteLine("Программа завершена.");
- inProgram = false;
- break;
- default:
- if (int.TryParse(userInput, out int num))
- {
- tempNumbers = new int[numbers.Length + 1];
- for (int i = 0; i < numbers.Length; i++)
- {
- tempNumbers[i] = numbers[i];
- }
- tempNumbers[tempNumbers.Length - 1] = num;
- numbers = tempNumbers;
- }
- else
- {
- Console.WriteLine("Ошибка ввода.");
- }
- break;
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement