Array Practice

mail@pastecode.io avatar
unknown
csharp
7 months ago
1.7 kB
22
Indexable
Never
static void ShowClassRoom(string[,] classRoom, bool isConfirm = false)
{
    for (int i = 0; i < classRoom.GetLength(0); i++)
    {
        for (int j = 0; j < classRoom.GetLength(1); j++)
        {
            if (isConfirm)
            {
                Console.ForegroundColor = ConsoleColor.Green;
            }

            Console.Write($"[{i + 1}, {j + 1}] " + classRoom[i, j] + " ");
            Console.ResetColor();
        }

        Console.WriteLine();
    }
}

string[,] classRoom = new string[4, 4]
{
    { "ПУСТО", "ПУСТО", "ПУСТО", "ПУСТО" },
    { "ПУСТО", "ПУСТО", "ПУСТО", "ПУСТО" },
    { "ПУСТО", "ПУСТО", "ПУСТО", "ПУСТО" },
    { "ПУСТО", "ПУСТО", "ПУСТО", "ПУСТО" },
};

while (true)
{
    Console.Clear();

    ShowClassRoom(classRoom);

    Console.WriteLine("================================");

    Console.Write("Введите ряд: ");
    int column = Convert.ToInt32(Console.ReadLine()) - 1;
    Console.Write("Введите место: ");
    int row = Convert.ToInt32(Console.ReadLine()) - 1;
    Console.Write("Введите имя: ");
    string name = Console.ReadLine();

    Console.WriteLine("================================");

    if (column >= classRoom.GetLength(0) || row >= classRoom.GetLength(1))
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("Не верно указано место или ряд");
        Console.ResetColor();

        Console.ReadKey();

        continue;
    }

    classRoom[column, row] = name;

    ShowClassRoom(classRoom, true);

    Console.ReadKey();
}
Leave a Comment