Shelves
unknown
csharp
a year ago
2.0 kB
7
Indexable
string[][] homeShelves = new string[][] { new string[] { "ПУСТО", "ТЕЛЕВИЗОР", "ПУСТО", "ТЕЛЕФОН", "ПУСТО" }, new string[] { "ПУСТО", "КНИГИ", "ПУСТО" } }; while (true) { Console.Clear(); Console.WriteLine("Выберите номер шкафа: "); int inputCurrentShelfIndex = Convert.ToInt32(Console.ReadLine()) - 1; if (inputCurrentShelfIndex >= 0 && inputCurrentShelfIndex < homeShelves.Length) { ShowShelves(homeShelves[inputCurrentShelfIndex]); InputData(out string item, out int shelfIndex); ValidData(homeShelves[inputCurrentShelfIndex], item, shelfIndex); Console.ReadKey(); Console.Clear(); ShowShelves(homeShelves[inputCurrentShelfIndex]); Console.WriteLine("Для продолжения нажмите любую клавишу"); Console.ReadKey(); } else { Console.WriteLine("Такого шкафа у нас нет"); Console.ReadKey(); continue; } } void ShowShelves(string[] shelves) { for (int i = 0; i < shelves.Length; i++) { string shelf = shelves[i]; Console.WriteLine($"Полка [{i + 1}]: {shelf}"); } } void InputData(out string item, out int shelfIndex) { Console.WriteLine("Какой предмет хотите добавить?"); item = Console.ReadLine(); Console.WriteLine("На какую полку?"); shelfIndex = Convert.ToInt32(Console.ReadLine()) - 1; } void ValidData(string[] shelves, string item, int shelfIndex) { if (shelfIndex >= 0 || shelfIndex < shelves.Length) { Console.WriteLine("Вы не верно указали полку"); return; } if (shelves[shelfIndex] != "ПУСТО") { Console.WriteLine("Полка не свободна"); } else { shelves[shelfIndex] = item; Console.WriteLine($"Вы добавили {item} на полку {shelfIndex + 1}"); } }
Editor is loading...
Leave a Comment