Created
April 27, 2024 12:33
-
-
Save trayforyou/b5f57e58c6ca3a68f49e3b4a357334bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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