Created
February 8, 2025 13:47
-
-
Save IlyaStvolov/bcf132b5bb09804056b8257f79507be3 to your computer and use it in GitHub Desktop.
ДЗ: Динамический массив
This file contains 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 Dynamic_Array | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string CommandSum = "sum"; | |
const string CommandExit = "exit"; | |
int[] numbers = new int[0]; | |
int sum; | |
bool isOpen = true; | |
string userUnput; | |
while (isOpen) | |
{ | |
Console.Write("Массив: "); | |
foreach (int number in numbers) | |
{ | |
Console.Write(number + " "); | |
} | |
Console.Write($"\n\nКомманда {CommandExit} - выход. Комманда {CommandSum} - сумма чисел в массиве."); | |
Console.Write("\nВведите число: "); | |
userUnput = Console.ReadLine().ToLower(); | |
switch (userUnput) | |
{ | |
case CommandSum: | |
sum = 0; | |
foreach (int number in numbers) | |
{ | |
sum += number; | |
} | |
Console.Clear(); | |
Console.SetCursorPosition(0, 1); | |
Console.WriteLine($"Сумма чисел = {sum}."); | |
Console.SetCursorPosition(0, 0); | |
break; | |
case CommandExit: | |
isOpen = false; | |
break; | |
default: | |
int[] tempNumbers = new int[numbers.Length + 1]; | |
for (int i = 0; i < numbers.Length; i++) | |
{ | |
tempNumbers[i] = numbers[i]; | |
} | |
tempNumbers[tempNumbers.Length - 1] = Convert.ToInt32(userUnput); | |
numbers = tempNumbers; | |
Console.Clear(); | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment