Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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# 并非所有代码路径都会为我的quickselect方法返回值错误_C# - Fatal编程技术网

C# 并非所有代码路径都会为我的quickselect方法返回值错误

C# 并非所有代码路径都会为我的quickselect方法返回值错误,c#,C#,下面的代码是我的quickselect(与quicksort非常相似,只是它返回排序数组中的第n项,而不是整个排序数组)方法。输入是字符串(单词)数组,如果给定的单词被排序,我必须返回第n个索引的单词 static string QuickSelect(string[] lst, int index) { if (lst.Length == 1) { Console.Write(lst[0]); return

下面的代码是我的quickselect(与quicksort非常相似,只是它返回排序数组中的第n项,而不是整个排序数组)方法。输入是字符串(单词)数组,如果给定的单词被排序,我必须返回第n个索引的单词

    static string QuickSelect(string[] lst, int index) {
        if (lst.Length == 1)
        {
            Console.Write(lst[0]);
            return lst[0];
        }

        else
        {
            int pivot = _r.Next(lst.Length); // pick a pivot by random number generator
            bool[] isDecre = new bool[lst.Length];
            string[] WordLeft, WordRight;
            int nLeft = 0, nRight = 0;

            for (int i = 0; i < lst.Length; i++) // compare values to pivot
            {
                if (i == pivot) continue;
                if (WordCompare(lst[pivot], lst[i]))
                {
                    nRight++;
                    isDecre[i] = false;
                }
                else
                {
                    nLeft++;
                    isDecre[i] = true;
                }
            }


            if (nLeft == index) // pivot was the (index)th item in the list
                return lst[pivot];

            else
            {

                WordLeft = new string[nLeft];
                WordRight = new string[nRight];
                int l = 0, r = 0;
                // divide list by saved comparison result
                for (int i = 0; i < lst.Length; i++)
                {
                    if (i == pivot) continue;
                    if (isDecre[i])
                    {
                        WordLeft[l] = lst[i];
                        l++;
                    }
                    else
                    {
                        WordRight[r] = lst[i];
                        r++;
                    }
                }

                if (nLeft > index)
                    return QuickSelect(WordLeft, index);
                else if (nLeft < index)
                    return QuickSelect(WordRight, index - nLeft - 1);
            }
        }
    }
静态字符串QuickSelect(字符串[]lst,int索引){
如果(lst.Length==1)
{
Console.Write(lst[0]);
返回lst[0];
}
其他的
{
int pivot=_r.Next(lst.Length);//通过随机数生成器选择一个轴
bool[]isDecre=新bool[lst.长度];
字符串[]WordLeft,WordRight;
int nLeft=0,nRight=0;
for(inti=0;i索引)
返回QuickSelect(WordLeft,index);
else if(nLeft<索引)
返回QuickSelect(WordRight,index-nLeft-1);
}
}
}
问题是此代码不会运行,抛出“并非所有代码路径都返回值”错误。我认为这与这部分有关:

if (nLeft == index) // pivot was the (index)th item in the list
    return lst[pivot];
// build left and right word lists
 ...
if (nLeft > index)
    return QuickSelect(WordLeft, index);
else if (nLeft < index)
    return QuickSelect(WordRight, index - nLeft - 1);
if(nLeft==index)//pivot是列表中的第(索引)项
返回lst[pivot];
//建立左右单词列表
...
如果(nLeft>索引)
返回QuickSelect(WordLeft,index);
else if(nLeft<索引)
返回QuickSelect(WordRight,index-nLeft-1);

如果我在“else if”之后加上一些“else”,错误就会消失。但当pivot是第n个索引字符串时,我不想构建左右单词列表。事实上,我认为检测到的这个非值返回路径有点胡说八道。有解决办法吗?

没有,没有解决办法

必须在方法中的所有路径中返回值或抛出异常(该方法不会处理该异常)

编辑:

另一方面,您的条件:

if (nLeft == index) // pivot was the (index)th item in the list
    return lst[pivot];
// build left and right word lists
 ...
if (nLeft > index)
    return QuickSelect(WordLeft, index);
else if (nLeft < index)
    return QuickSelect(WordRight, index - nLeft - 1);

if(nLeft>index)
返回QuickSelect(WordLeft,index)
如果(nLeft 返回QuickSelect(WordRight,index-nLeft-1)
else//nLeft==索引
返回“某物”


如果两个条件都失败,那么您仍然需要返回一些值,您可以根据返回值进一步处理该情况。

您的代码只会在指定的条件为真时返回该值。由于您指定了三个条件,因此该方法仅在其中一个条件变为true时返回一个值,但在这种情况下,您将收到一个错误,因为如果这三个条件都变为false,应用程序将不知道该怎么办。由于您尚未指定当这些条件变为false时将执行的操作,因此不会返回任何值,这就是为什么您会收到一个错误,因为bool可以是
true
false

if (nLeft == index) //Continue if the condition becomes true
    return lst[pivot]; //Return a value
 ...
if (nLeft > index) //Continue if the condition becomes true
    return QuickSelect(WordLeft, index); //Return a value
else if (nLeft < index) //Continue if the condition becomes true
    return QuickSelect(WordRight, index - nLeft - 1); //Return a value
谢谢,

我希望这对您有所帮助:)

您甚至可以删除else,因为if的内容是退货。完全解决了。顺便说一句,我更喜欢使用“else”关键字,这不会稍微提高可读性吗?还是有一些超速处罚?@MarcinJuraszek,我知道。我只需要等一会儿就可以接受答案。谢谢你的快速建议。或者你可以使用
return(nLeft>index)?QuickSelect(WordLeft,index):QuickSelect(WordRight,index-nLeft-1)
如果
nLeft==index
您不返回值,那么很简单。
if (nLeft == index) //Continue if the condition becomes true
    return lst[pivot]; //Return a value
 ...
if (nLeft > index) //Continue if the condition becomes true
    return QuickSelect(WordLeft, index); //Return a value
else if (nLeft < index) //Continue if the condition becomes true
    return QuickSelect(WordRight, index - nLeft - 1); //Return a value
if (nLeft == index) //Continue if the condition becomes true
    return lst[pivot]; //Return a value
 ...
if (nLeft > index) //Continue if the condition becomes true
    return QuickSelect(WordLeft, index); //Return a value
else if (nLeft < index) //Continue if the condition becomes true
    return QuickSelect(WordRight, index - nLeft - 1); //Return a value
else //Continue if these conditions become false
    return /* value */; //Return a value