Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- const string ExitCommand = "exit";
- const string SumCommand = "sum";
- List<int> numbers = new List<int>();
- bool isWorking = true;
- while (isWorking)
- {
- Console.Clear();
- Console.Write("Массив: ");
- ShowArray(numbers);
- Console.Write($"\n1)Введите '{SumCommand}' для суммирования чисел" +
- $"\n2)Введите '{ExitCommand}' для выхода");
- Console.Write("\nВведите число или команду: ");
- string inputMessage = Console.ReadLine().ToLower();
- switch (inputMessage)
- {
- case SumCommand:
- SumNumber(numbers);
- break;
- case ExitCommand:
- isWorking = false;
- break;
- default:
- AddNumber(inputMessage, numbers);
- break;
- }
- Console.WriteLine("Нажмите любую клавишу...");
- Console.ReadLine();
- }
- }
- private static void SumNumber(List<int> numbers)
- {
- int sum = 0;
- foreach (int numberInList in numbers)
- {
- sum += numberInList;
- }
- Console.WriteLine($"\nСумма введёных чисел: {sum}");
- }
- private static void AddNumber(string inputMessage, List<int> numbers)
- {
- if (int.TryParse(inputMessage, out int number))
- {
- numbers.Add(number);
- }
- else
- {
- Console.WriteLine("\nНекорректный ввод\n");
- }
- }
- private static void ShowArray(List<int> numbers)
- {
- for (int i = 0; i < numbers.Count; i++)
- {
- Console.Write(numbers[i] + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement