【发布时间】:2020-03-26 07:21:29
【问题描述】:
这里是编程新手。
我在此项目中有一项要求,即检查输入的重复球员号码(例如 Michael Jordan 23)。不能让 Dave Brown 23,因为 Michael 已经分配了那个号码。
我需要帮助来理解这段代码中发生了什么。
这是我下面的内容。我评论了我认为正在发生的事情。
static int SearchNumber(int[] playerNumbers, int newPlayerNumber, int playerCount)
{
//3. //
int index = 0;
bool found = false; //found duplicate number = not true
while (!found && index < playerCount)//while we did find a duplicate number && index < playerCount
{
if (playerNumbers[index] == newPlayerNumber)
{
found = true; // we found a duplicate number
}
else //we did not find a duplicate number
{
index++; //add onto the team
}
}
if (!found) //if we did find a duplicate number
{
index = -1; //subtract 1 from the team. return to menu
}
return index;
}
下面是我不明白的部分:
Screen shot of code with highlighted to understand what I don't understand
谢谢。
【问题讨论】:
-
-1 是一些程序用来指定“我们没有找到索引”同时保证返回值是整数的约定。
-
旁注:
SearchNumber对该方法应该做什么的描述非常糟糕。投资于能够准确描述其功能的命名方法,您将花费更少的时间找出代码。 -
设置
bool found = false是在说“我假设我们没有找到它,但如果我找到它,我将设置found = true。” -
@JJLe 该网站仅在您接受答案并为您喜欢的答案投票时才有用。让问题悬而未决无济于事
标签: c#