Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp3
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- const string CommandSum = "sum";
- const string CommandExit = "exit";
- const string CommandPrint = "print";
- bool isRuning = true;
- List<int> numbers = new List<int>();
- while (isRuning)
- {
- Console.Write("Введите команду или число: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandSum:
- SumNumbers(numbers);
- break;
- case CommandExit:
- isRuning = false;
- break;
- case CommandPrint:
- PrintNumbers(numbers);
- break;
- default:
- AddNumber(numbers, userInput);
- break;
- }
- }
- }
- static void SumNumbers(List<int> numbers)
- {
- int sum = 0;
- foreach (int number in numbers)
- sum += number;
- Console.WriteLine("Сумма чисел равна " + sum);
- }
- static void AddNumber(List<int> numbers, string userInput)
- {
- if (int.TryParse(userInput, out int number))
- numbers.Add(number);
- else
- Console.WriteLine("Введите целое число!");
- }
- static void PrintNumbers(List<int> numbers)
- {
- foreach (int number in numbers)
- Console.Write(number + " ");
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement