W1thr

Массивы - 4

Feb 13th, 2021 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int[] array = new int[1];
  15.             bool isActive = true;
  16.             int sum = 0;
  17.  
  18.             Console.WriteLine("Введите любое число\n" +
  19.             "sum - вывод суммы всех введеных ранее чисел\n" +
  20.             "exit - выход");
  21.             string userInput;
  22.  
  23.             while (isActive)
  24.             {
  25.                 userInput = Console.ReadLine();
  26.  
  27.                 switch (userInput)
  28.                 {
  29.                     case "exit":
  30.                         isActive = false;
  31.                         break;
  32.                     case "sum":
  33.                         foreach (int element in array)
  34.                         {
  35.                             sum += element;
  36.                         }
  37.                         Console.WriteLine(sum);
  38.                         sum = 0;
  39.                         break;
  40.                     default:
  41.                         int[] tempArray = new int[array.Length + 1];
  42.  
  43.                         for (int i = 0; i < array.Length; i++)
  44.                         {
  45.                             tempArray[i] = array[i];
  46.                         }
  47.                         tempArray[tempArray.Length - 1] = Convert.ToInt32(userInput);
  48.                         array = tempArray;
  49.                         break;
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment