Created
May 1, 2024 06:16
-
-
Save NoDamage111/6206c8b5cee4a15b53021334b301f556 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 ConsoleApp24 | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
List<int> numbers = new List<int>(); | |
string userInput; | |
bool isWorking = true; | |
while (isWorking) | |
{ | |
const string CommandSum = "sum"; | |
const string CommandExit = "exit"; | |
Console.Write("\"exit\" для завершения программы" + | |
"\n\"sum\" для вывода суммы всех чисел в списке\n\n" + | |
"Введите число для добавления в список:"); | |
userInput = Console.ReadLine(); | |
switch (userInput) | |
{ | |
case CommandSum: | |
CalculateSum(numbers); | |
break; | |
case CommandExit: | |
isWorking = false; | |
break; | |
default: | |
AddNumber(ref numbers, userInput); | |
break; | |
} | |
} | |
} | |
static void AddNumber(ref List<int> numbers, string userInput) | |
{ | |
if (int.TryParse(userInput, out int result)) | |
{ | |
numbers.Add(result); | |
} | |
else | |
{ | |
Console.WriteLine("Вы ввели не число"); | |
} | |
} | |
static void CalculateSum(List<int> numbers) | |
{ | |
int sum = 0; | |
foreach (var number in numbers) | |
{ | |
sum += number; | |
} | |
Console.WriteLine($"Сумма всех чиел в списке равна: {sum}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment