Skip to content

Instantly share code, notes, and snippets.

@trayforyou
Created April 27, 2024 12:33
Show Gist options
  • Save trayforyou/b5f57e58c6ca3a68f49e3b4a357334bd to your computer and use it in GitHub Desktop.
Save trayforyou/b5f57e58c6ca3a68f49e3b4a357334bd to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Collections2
{
internal class Program
{
static void Main(string[] args)
{
bool isOpen = true;
string commandExit = "exit";
string commandSum = "sum";
string userInput;
int userInputNumber;
List<int> numbers = new List<int>();
while (isOpen)
{
Console.Clear();
Console.Write("Ввеите число или команду: ");
userInput = Console.ReadLine();
if (userInput == commandSum)
{
WriteSum(numbers);
}
else if (userInput == commandExit)
{
isOpen = false;
}
else if (int.TryParse(userInput, out userInputNumber))
{
numbers.Add(userInputNumber);
}
Console.ReadKey();
}
}
private static void WriteSum(List<int> numbers)
{
int sum = 0;
foreach (int number in numbers)
{
sum += number;
}
Console.WriteLine($"Сумма всех чисел равна: {sum}.");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment