Askor

Hw26

Nov 18th, 2020 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Test
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<int> numbers = new List<int>();
  11.             int sum = 0;
  12.  
  13.             Console.WriteLine("Чтобы начать нажмите любую клавишу...");
  14.             string userInput = Console.ReadLine();
  15.            
  16.             Console.WriteLine("Для суммы введите - sum | Для выхода из программы введите - exit");
  17.  
  18.             while (userInput != "exit")
  19.             {
  20.                 Console.WriteLine("Введите число:");
  21.                 userInput = Console.ReadLine();
  22.                 bool isNum = int.TryParse(userInput, out sum);
  23.  
  24.                 if (isNum)
  25.                 {
  26.                     numbers.Add(Convert.ToInt32(userInput));
  27.                 }
  28.                 else if (userInput == "sum")
  29.                 {  
  30.                     foreach (var number in numbers)
  31.                     {
  32.                         sum += number;
  33.                     }
  34.  
  35.                     Console.WriteLine($"Сумма: {sum}");
  36.                     sum = 0;
  37.                 }
  38.                 else if (userInput != "exit")
  39.                 {
  40.                     Console.WriteLine("Вы ввели неправильное значение.");
  41.                 }
  42.             }
  43.         }
  44.     }
  45. }
  46.  
Add Comment
Please, Sign In to add comment