Advertisement
JohnJuly

Homework35

Feb 25th, 2024 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Homework35
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             const string CommandSum = "sum";
  14.             const string CommandExit = "exit";
  15.  
  16.             List<int> inputNumbers = new List<int>();
  17.             string userInput;
  18.             bool isOpen = true;
  19.  
  20.             while (isOpen)
  21.             {
  22.                 Console.WriteLine("Вводите числа в программу.");
  23.                 Console.WriteLine($"Чтобы получить сумму всех введенных чисел введите команду: {CommandSum}");
  24.                 Console.WriteLine($"Чтобы выйти из программы введите команду: {CommandExit}");
  25.  
  26.                 userInput = Console.ReadLine();
  27.  
  28.                 if (userInput == CommandSum)
  29.                 {
  30.                     SumInputNumbers(inputNumbers);
  31.                 }
  32.                 else if (userInput == CommandExit)
  33.                 {
  34.                     isOpen = ExitProgram();
  35.                 }
  36.                 else if (int.TryParse(userInput, out int result))
  37.                 {
  38.                     inputNumbers.Add(result);
  39.                 }
  40.  
  41.                 Console.Clear();
  42.             }
  43.         }
  44.  
  45.         static void SumInputNumbers(List<int> inputNumbers)
  46.         {
  47.             int result = 0;
  48.  
  49.             foreach (var number in inputNumbers)
  50.             {
  51.                 result += number;
  52.             }
  53.  
  54.             Console.WriteLine($"Cумма чисел равна : {result}");
  55.             Console.ReadKey();
  56.         }
  57.  
  58.         static bool ExitProgram()
  59.         {
  60.             Console.WriteLine("Вы вышли из программы!");
  61.  
  62.             return false;
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement