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 Homework35
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- const string CommandSum = "sum";
- const string CommandExit = "exit";
- List<int> inputNumbers = new List<int>();
- string userInput;
- bool isOpen = true;
- while (isOpen)
- {
- Console.WriteLine("Вводите числа в программу.");
- Console.WriteLine($"Чтобы получить сумму всех введенных чисел введите команду: {CommandSum}");
- Console.WriteLine($"Чтобы выйти из программы введите команду: {CommandExit}");
- userInput = Console.ReadLine();
- if (userInput == CommandSum)
- {
- SumInputNumbers(inputNumbers);
- }
- else if (userInput == CommandExit)
- {
- isOpen = ExitProgram();
- }
- else if (int.TryParse(userInput, out int result))
- {
- inputNumbers.Add(result);
- }
- Console.Clear();
- }
- }
- static void SumInputNumbers(List<int> inputNumbers)
- {
- int result = 0;
- foreach (var number in inputNumbers)
- {
- result += number;
- }
- Console.WriteLine($"Cумма чисел равна : {result}");
- Console.ReadKey();
- }
- static bool ExitProgram()
- {
- Console.WriteLine("Вы вышли из программы!");
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement