возникли проблемы с удалением данных из массивов - PullRequest
0 голосов
/ 26 марта 2019

У меня возникли проблемы с поиском в массиве слова "имя торгового представителя", затем удаление имени, продажи и комиссии.

static void RemoveSale(string[] sellerNames, double[] sellerSales, double[] sellerCommision, ref int sellerCount)
    {
        int index;
        string salesRep;
        try
        {
            if (sellerCount > 0)
            {
                salesRep = GetValidName("Enter the sales rep to delete");
                index = SearchSeller(sellerNames, sellerCount);
                if (index == -1)
                {
                    Console.WriteLine("that sales rep is not on the team");
                }
                else
                {
                    sellerSales[index] = sellerSales[sellerCount - 1];
                    sellerNames[index] = sellerNames[sellerCount - 1];
                    sellerCommision[index] = sellerCommision[sellerCount - 1];
                    Console.WriteLine("Sales rep {0} has been deleted.", sellerNames);
                    sellerCount--;
                }
            }
            else
            {
                Console.WriteLine("there are no sales reps to delete");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }


    static int SearchSeller(string[] sellerNames, int sellerCount)
    {
        try
        {
            int index = 0;
            bool found = false;
                            while (!found && index < sellerCount)
            {
                if (sellerNames[sellerCount] == sellerNames[index])
                    found = true;
                else
                    index++;
            }
            if (!found)
                index = -1;
            return index;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        return 0;
        }

    }

Ожидаемый результат - удаление имени, данных и подтверждение того, что sellerName {0} былоудалено.

общий результат - ничего не удалено, и нет подтверждения удаленного продавца

1 Ответ

1 голос
/ 26 марта 2019

Вы не передаете свой объект salesRep в функцию поиска.Предполагая, что вы можете изменить сигнатуру функции поиска, попробуйте следующее:

// Pass the name of the sales rep
static int SearchSeller(string[] sellerNames, int sellerCount, string salesRep)
    {
        try
        {
            int index = 0;
            bool found = false;
                            while (!found && index < sellerCount)
            {
                // compare the current name in array with the passed name
                if (salesRep == sellerNames[index])
                    found = true;
                else
                    index++;
            }
            if (!found)
                index = -1;
            return index;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        return 0;
        }

Отладка обычно должна быть первым шагом для проверки более простых ошибок.

Лучшим вариантом поиска будетиспользуйте функцию c # Find

Array.Find(sellerNames, salesRep);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...