Advertisement
TeT91

ДЗ Динамический массив 2

May 23rd, 2024 (edited)
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const string CommandSum = "Sum";
  11.             const string CommandExit = "Exit";
  12.  
  13.             List<int> numbers = new List<int>();
  14.  
  15.             bool isInProgress = true;
  16.  
  17.             while (isInProgress)
  18.             {
  19.                 if (numbers.Count > 0)
  20.                 {
  21.                     Console.WriteLine("Числа в массиве");
  22.  
  23.                     for (int i = 0; i < numbers.Count; i++)
  24.                     {
  25.                         Console.Write(numbers[i] + " ");
  26.                     }
  27.                 }
  28.  
  29.                 Console.WriteLine();
  30.                 Console.WriteLine("Введите команду или число");
  31.                 string userInput = Console.ReadLine();
  32.  
  33.                 switch (userInput)
  34.                 {
  35.                     case CommandSum:
  36.                         int sum = 0;
  37.  
  38.                         foreach (int number in numbers)
  39.                         {
  40.                             sum += number;
  41.                         }
  42.  
  43.                         Console.WriteLine($"Сумма всех чисел = {sum}");
  44.                         break;
  45.  
  46.                     case CommandExit:
  47.                         isInProgress = false;
  48.                         break;
  49.  
  50.                     default:
  51.                         if (int.TryParse(userInput, out int parsedNumber))
  52.                         {
  53.                             numbers.Add(parsedNumber);
  54.  
  55.                             Console.WriteLine();
  56.                         }
  57.                         else
  58.                         {
  59.                             Console.WriteLine("Неизвестная команда");
  60.                         }
  61.                         break;
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement