Advertisement
Rodunskiy

Untitled

May 11th, 2025
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. namespace CSLight
  2. {
  3.     public class Program
  4.     {
  5.         private static List<double> numbers = new List<double>();
  6.         private const string ExitCommand = "exit";
  7.         private const string SumCommand = "sum";
  8.  
  9.         static void Main(string[] args)
  10.         {
  11.             bool isWorking = true;
  12.  
  13.             while (isWorking)
  14.             {
  15.                 DisplayNumbers();
  16.                 string input = GetUserInput();
  17.  
  18.                 switch (input)
  19.                 {
  20.                     case ExitCommand:
  21.                         ExitProgram(ref isWorking);
  22.                         break;
  23.  
  24.                     case SumCommand:
  25.                         CalculateAndDisplaySum();
  26.                         break;
  27.  
  28.                     default:
  29.                         TryAddNumber(input);
  30.                         break;
  31.                 }
  32.             }
  33.         }
  34.  
  35.         static void DisplayNumbers()
  36.         {
  37.             Console.WriteLine("Введенные числа: " + string.Join(", ", numbers));
  38.         }
  39.  
  40.         static string GetUserInput()
  41.         {
  42.             Console.Write($"Введите число или команду {SumCommand}/{ExitCommand}: ");
  43.             return Console.ReadLine();
  44.         }
  45.  
  46.         static void ExitProgram(ref bool isWorking)
  47.         {
  48.             Console.WriteLine("Выход из программы.");
  49.             isWorking = false;
  50.         }
  51.  
  52.         static void CalculateAndDisplaySum()
  53.         {
  54.             double sum = 0;
  55.  
  56.             foreach (double number in numbers)
  57.             {
  58.                 sum += number;
  59.             }
  60.  
  61.             Console.WriteLine($"Сумма введенных чисел: {sum}");
  62.         }
  63.  
  64.         static void TryAddNumber(string input)
  65.         {
  66.             if (double.TryParse(input, out double number))
  67.             {
  68.                 numbers.Add(number);
  69.                 Console.WriteLine($"Число {number} добавлено.");
  70.             }
  71.             else
  72.             {
  73.                 Console.WriteLine($"Некорректный ввод. Пожалуйста, введите число или команду {SumCommand}/{ExitCommand}.");
  74.             }
  75.         }
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement