Advertisement
Dr_Max_Experience

Task 18

Aug 18th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 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 Homework_1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = new int[1];
  14.             bool isActive = true;
  15.  
  16.             while (isActive)
  17.             {
  18.                 Console.Write($"Введите 'sum' или {array.Length}-й элемент массива: ");
  19.                 string userInput = Console.ReadLine();
  20.  
  21.                 if (userInput == "exit")
  22.                 {
  23.                     isActive = false;
  24.                 }
  25.                 else if (userInput == "sum")
  26.                 {
  27.                     Console.Clear();
  28.                     int sum = 0;
  29.  
  30.                     for (int i = 0; i < array.Length; i++)
  31.                     {
  32.                         sum += array[i];
  33.                     }
  34.                     Console.WriteLine($"Сумма всех элементов массива = {sum}");
  35.                     Console.ReadKey();
  36.                     Console.Clear();
  37.                 }
  38.                 else
  39.                 {
  40.                     int[] tempArray = new int[array.Length + 1];
  41.  
  42.                     for (int i = 0; i < array.Length; i++)
  43.                     {
  44.                         tempArray[i] = array[i];
  45.                     }
  46.                     tempArray[tempArray.Length - 1] = Convert.ToInt32(userInput);
  47.                     array = tempArray;
  48.                     Console.Clear();
  49.                 }
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement