Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace array
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string CommandSum = "sum";
- const string CommandExit = "exit";
- bool isRunning = true;
- List<int> numbers = new List<int>();
- string userInput;
- Console.WriteLine($"{CommandSum} Подсчитать сумму введённых чисел");
- Console.WriteLine($"{CommandExit} Завершить рабту программы");
- Console.WriteLine($"<число> Добавить в память число");
- while (isRunning)
- {
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandExit:
- isRunning = false;
- break;
- case CommandSum:
- CalculateSum(numbers);
- break;
- default:
- AddNumber(userInput, numbers);
- break;
- }
- }
- }
- public static void CalculateSum(List<int> numbers)
- {
- int sum = 0;
- foreach (int number in numbers)
- sum += number;
- Console.WriteLine($"Сумма элементов массива: {sum}");
- }
- public static void AddNumber(string userCommand, List<int> numbers)
- {
- int number;
- bool isNumber = int.TryParse(userCommand, out number);
- if (isNumber)
- numbers.Add(number);
- else
- Console.WriteLine("Не удалось распознать ввод");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement