Last active
September 6, 2025 17:19
-
-
Save Attosius/3b3184b9bed4858b0b12a10a11abd062 to your computer and use it in GitHub Desktop.
ДЗ: Динамический массив
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace IJuniorTasks | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string CommandSum = "sum"; | |
const string CommandExit = "exit"; | |
bool isWork = true; | |
int[] userInputNumbers = new int[0]; | |
while (isWork) | |
{ | |
Console.WriteLine($"Текущий массив:"); | |
foreach (var item in userInputNumbers) | |
{ | |
Console.Write($"{item} "); | |
} | |
if (userInputNumbers.Length == 0) | |
{ | |
Console.Write($"Пустой массив"); | |
} | |
Console.WriteLine($"\n\nВведите команду:"); | |
Console.WriteLine($"{CommandSum}. Вывести сумму"); | |
Console.WriteLine($"{CommandExit}. Выход"); | |
Console.WriteLine($"Любое число - записать число в массив"); | |
Console.WriteLine(); | |
var command = Console.ReadLine(); | |
switch (command) | |
{ | |
case CommandSum: | |
var sum = 0; | |
for (int i = 0; i < userInputNumbers.Length; i++) | |
{ | |
sum += userInputNumbers[i]; | |
} | |
Console.WriteLine($"Сумма введенных чисел: {sum}"); | |
break; | |
case CommandExit: | |
isWork = false; | |
break; | |
default: | |
var userInputNumber = Convert.ToInt32(command); | |
var temp = userInputNumbers; | |
userInputNumbers = new int[temp.Length + 1]; | |
for (int i = 0; i < temp.Length; i++) | |
{ | |
userInputNumbers[i] = temp[i]; | |
} | |
userInputNumbers[userInputNumbers.Length - 1] = userInputNumber; | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment