Advertisement
AziLif

Динамический массив

Nov 26th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] numbers = new int[0];
  10.  
  11.             string userInput;
  12.             string sum = "sum";
  13.             string exit = "exit";
  14.  
  15.             bool isFillingArray = true;
  16.  
  17.             int newAddedNumber = 0;
  18.             int resultAddition = 0;
  19.  
  20.             Console.WriteLine("Сначала наш массив будет пуст((.");
  21.  
  22.             for (int i = 0; i < numbers.Length; i++)
  23.             {
  24.                 Console.Write(numbers[i] + " ");
  25.             }
  26.  
  27.             Console.WriteLine($"\nДальше вам нужно выбрать что вы делаете. Первое - вы можете написать {sum} - и получить сумму написанных чисел, " +
  28.                 $"вы можете написать {exit} и программа завершися, либо продолжить заполнение массива(любой знак или слово будут " +
  29.                 $"добавлены в массив если это не {sum} или {exit}).");
  30.  
  31.             while (isFillingArray)
  32.             {
  33.                 userInput = Console.ReadLine();
  34.  
  35.                 if (userInput == sum)
  36.                 {
  37.  
  38.                     for (int i = 0; i < numbers.Length; i++)
  39.                     {
  40.                         resultAddition += numbers[i];
  41.                     }
  42.  
  43.                     Console.WriteLine($"Сумма массива - {resultAddition}.");
  44.                 }
  45.                 else if (userInput == exit)
  46.                 {
  47.                     isFillingArray = false;
  48.                     Console.WriteLine("Спасибо что работали с нами.");
  49.                 }
  50.                 else
  51.                 {
  52.                     newAddedNumber = int.Parse(userInput);
  53.                     int[] newEnlargedArray = new int[numbers.Length + 1];
  54.  
  55.                     for (int i = 0; i < numbers.Length; i++)
  56.                     {
  57.                         newEnlargedArray[i] = numbers[i];
  58.                         Console.WriteLine(newEnlargedArray[i]);
  59.                     }
  60.  
  61.                     newEnlargedArray[newEnlargedArray.Length - 1] = newAddedNumber;
  62.                     numbers = newEnlargedArray;
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement