W1thr

Коллекции-3

Mar 6th, 2021 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 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 Homework3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool exit = false;
  14.             List<int> numbers = new List<int>();
  15.  
  16.             while (exit == false)
  17.             {
  18.                 Console.Write("Введите число или команду:");
  19.                 string userInput = Console.ReadLine();
  20.  
  21.                 if(int.TryParse(userInput, out int result))
  22.                 {
  23.                     numbers.Add(result);
  24.                 }
  25.                 else if (userInput == "exit")
  26.                 {
  27.                     exit = true;
  28.                 }
  29.                 else if(userInput == "sum")
  30.                 {
  31.                     int sum = 0;
  32.                     foreach (var number in numbers)
  33.                     {
  34.                         sum += number;
  35.                     }
  36.                     Console.WriteLine(sum);
  37.                 }
  38.                 else
  39.                 {
  40.                     Console.WriteLine("Это не число!");
  41.                 }
  42.             }
  43.         }
  44.     }
  45. }
  46.  
Add Comment
Please, Sign In to add comment