Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CollectionsTask3DynamicArray
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- const string CommandExit = "exit";
- const string CommandSum = "sum";
- const string CommandNew = "new";
- List <int> numbers = new List <int> ();
- bool isRunning = true;
- string userInput;
- Console.WriteLine($"Здравствуйте! Введите число для добавления в массив или команды {CommandExit}, {CommandSum} или {CommandNew}!");
- while (isRunning)
- {
- PrintList(numbers);
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandExit:
- isRunning = false;
- break;
- case CommandNew:
- numbers.Clear ();
- break;
- case CommandSum:
- Console.WriteLine($"Сумма элементов списка: {CalcSum(numbers)}");
- break;
- default:
- ConvertAndAddIfElementIsNumber(userInput, numbers);
- break;
- }
- }
- }
- private static void PrintList(List<int> list)
- {
- foreach (int item in list)
- {
- Console.Write($"{item} ");
- }
- Console.WriteLine();
- }
- private static int CalcSum(List<int> list)
- {
- int sum = 0;
- foreach (int item in list)
- {
- sum += item;
- }
- return sum;
- }
- private static void ConvertAndAddIfElementIsNumber(string element, List <int> list)
- {
- int inputNumber;
- if (int.TryParse(element, out inputNumber))
- {
- list.Add(inputNumber);
- }
- else
- {
- Console.WriteLine("Введено не число.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement