【问题标题】:Stuck at searching and removing from array卡在搜索和从数组中删除
【发布时间】:2019-08-14 09:43:49
【问题描述】:

必须添加销售代表姓名、金额、计算佣金,并从数组中删除销售代表和销售。

我正在努力使用 searchSeller 方法,并且在超过 24 小时内没有取得任何进展。

static string SearchSeller(int[] sellerSales, string[] sellerNames, int sellerCount,
                           ref string salesRep)
{
    int index = 0;
    bool found = false;

    while (!found && index < sellerCount)
    {
        if (salesRep = sellerNames[index])
            found = true;
        else
            index++;
    }
    if (!found)
        index = -1;
    return sellerNames[index];
}

问题似乎出在某个地方:

if (salesRep = sellerNames[index])

错误提示:

无法将字符串转换为布尔值。

【问题讨论】:

  • 欢迎来到 Stack Overflow。这是一个很好的第一个问题。 :)
  • 顺便说一句,您应该检查 Array.FindIndex() 或者 Linq 的 Enumerable.First()、Enumerable.Single() 和 Enumerable.Contains 。 Try it online

标签: c# arrays methods


【解决方案1】:

改变

 if (salesRep = sellerNames[index])

 if (salesRep == sellerNames[index])

单个= 会将sellerNames[index] 分配给salesRep,而== 用于比较

因为你已经给出了要搜索的文本,所以返回相同的值没有任何意义,我想你会想要返回它的索引吧?

return index; //indested of return sellerNames[index];

你的函数应该返回int:

static int SearchSeller(int[] sellerSales, string[] sellerNames, int sellerCount, ref string salesRep)

【讨论】:

  • 哇。我已经为此疯狂了一天多!最简单的事情。谢谢你!
  • @electric.arc:你可以简化你的逻辑:int index = 0; while (index &lt; sellerCount) { if (salesRep == sellerNames[index]) return index; index++; } return -1;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
相关资源
最近更新 更多