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