Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我需要帮助理解bool=false和-1的含义_C# - Fatal编程技术网

C# 我需要帮助理解bool=false和-1的含义

C# 我需要帮助理解bool=false和-1的含义,c#,C#,这里是编程新手 我有一个在这个项目的要求,检查重复的球员号码输入(如迈克尔乔丹23)。不能让Dave Brown 23,因为Michael已经分配了该号码 我需要帮助理解这段代码中发生了什么 下面是我的资料。我把我认为正在发生的事情说了出来 static int SearchNumber(int[] playerNumbers, int newPlayerNumber, int playerCount) { //3. // int index =

这里是编程新手

我有一个在这个项目的要求,检查重复的球员号码输入(如迈克尔乔丹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;
    }
static int SearchNumber(int[]playerNumber,int newPlayerNumber,int playerCount)
{
//3. //
int指数=0;
bool found=false;//found replicate number=not true
while(!found&&index
以下是我不理解的部分:

谢谢。


从一条评论中删除了这条信息,但我觉得这个问题本身不够强烈,我不愿意在上面写上我的名字


返回
-1
是一些程序用来指定“我们没有找到索引”的约定,同时保证返回值是整数

例如,将始终返回一个整数-找到匹配项时返回正索引,否则返回-1


我只能推测原因,但根据我的经验,我见过较旧的程序和API使用此方法,而较新的程序或抛出异常或返回可为空的整数。

这应该是对此代码的正确注释

static int SearchNumber(int[]playerNumber,int newPlayerNumber,int playerCount)
{
int index=0;//迭代索引
bool found=false;//当玩家被找到时,旗帜升起
while(!found&&index
现在用同样的方法对.NET进行优化

static int SearchNumber(int[]playerNumber,int newPlayerNumber)
{
int len=播放编号。长度;
对于(int i=0;i
-1是一些程序用来指定“我们没有找到索引”的约定,同时保证返回值是整数。例如,请注意:
SearchNumber
对该方法应该做什么的描述非常糟糕。投资于能够准确描述其行为的命名方法,你将花更少的时间找出代码。设置
bool found=false
是一种表达“我假设我们还没有找到它,但如果我找到了,我将设置
found=true
”@JJLe这个网站只有在你接受答案并投票选出你喜欢的答案时才有用。把问题留着不谈也无济于事