在搜索阵列中“销售代表的姓名”,然后删除名称,销售和佣金时遇到问题。
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}已被删除。
最终结果是什么也没有被删除,也没有确认被删除的卖家
答案 0 :(得分:1)
您没有将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);