Advertisement
Fle2x

5.3.1

Jun 15th, 2021
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             bool isExit = false;
  11.             List<int> numbers = new List<int>();
  12.             while (!isExit)
  13.             {
  14.                 string input = Console.ReadLine();
  15.                 switch (input)
  16.                 {
  17.                     case "exit":
  18.                         isExit = true;
  19.                         break;
  20.                     case "sum":
  21.                         int sum = 0;
  22.                         foreach (var number in numbers)
  23.                         {
  24.                             sum += number;
  25.                         }
  26.  
  27.                         Console.WriteLine("Сумма всех веденных чисел: " + sum);
  28.                         break;
  29.                     default:
  30.                         int isNumber;
  31.                         bool success = Int32.TryParse(input, out isNumber);
  32.  
  33.                         if (success)
  34.                         {
  35.                             numbers.Add(Convert.ToInt32(input));
  36.                         }
  37.                         else
  38.                         {
  39.                             Console.WriteLine("Введите число!");
  40.                         }
  41.  
  42.                         break;
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement