Dr_Max_Experience

Task 27

Nov 28th, 2021 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. namespace HomeWorks
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             bool isOpen = true;
  13.             string userInput;
  14.             bool isNumber;
  15.  
  16.             List<int> values = new List<int>();
  17.  
  18.             while (isOpen)
  19.             {
  20.                 Console.Clear();
  21.                 Console.Write("Введите комманду sum или число: ");
  22.                 userInput = Console.ReadLine();
  23.  
  24.                 if (userInput == "exit")
  25.                 {
  26.                     isOpen = false;
  27.                 }
  28.                 else if (userInput == "sum")
  29.                 {
  30.                     int sum = 0;
  31.  
  32.                     foreach (var value in values)
  33.                     {
  34.                         sum += value;
  35.                     }
  36.                     Console.WriteLine($"Сумма введённых чисел = {sum}");
  37.                     values.Clear();
  38.  
  39.                     Console.ReadKey();
  40.                 }
  41.                 else
  42.                 {
  43.                     CheckCorrectnessInput(userInput, out isNumber);
  44.                     if (isNumber)
  45.                     {
  46.                         values.Add(Convert.ToInt32(userInput));
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.  
  52.         static void CheckCorrectnessInput(string userInput, out bool isNumber)
  53.         {
  54.             isNumber = int.TryParse(userInput, out int intValue);
  55.  
  56.             if (isNumber == false)
  57.             {
  58.                 Console.Write("Некорректный ввод!");
  59.             }
  60.         }
  61.     }
  62. }
Add Comment
Please, Sign In to add comment