Last active
May 1, 2024 04:03
-
-
Save FleamRus/24fbc333d51897cab92f6997ee428787 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 Динамический_массив | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string ComandExit = "exit"; | |
const string ComandSum = "sum"; | |
int[] numbers = new int[0]; | |
string userInput; | |
bool isWork = true; | |
while (isWork) | |
{ | |
Console.WriteLine("\nВведите команду:"); | |
Console.WriteLine($"{ComandSum} - посчитать сумму всех чисел в массиве"); | |
Console.WriteLine($"{ComandExit} - завершение программы"); | |
Console.WriteLine($"Либо введите число для добавления его в массив"); | |
userInput = Console.ReadLine(); | |
Console.Clear(); | |
switch (userInput) | |
{ | |
case ComandSum: | |
int sumNumbers = 0; | |
for (int i = 0; i < numbers.Length; i++) | |
{ | |
sumNumbers += numbers[i]; | |
} | |
Console.WriteLine("Сумма чисел массива: " + sumNumbers); | |
break; | |
case ComandExit: | |
isWork = false; | |
Console.WriteLine("Программа завершена"); | |
break; | |
default: | |
int[] tempNumber = new int[numbers.Length + 1]; | |
tempNumber[tempNumber.Length - 1] = Convert.ToInt32(userInput); | |
for (int i = 0; i < numbers.Length; i++) | |
{ | |
tempNumber[i] = numbers[i]; | |
} | |
numbers = tempNumber; | |
Console.WriteLine("Число довавлено в массив."); | |
break; | |
} | |
Console.WriteLine("Массив содержит числа: "); | |
for (int i = 0; i < numbers.Length; i++) | |
{ | |
Console.Write(numbers[i] + " "); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment