如何解决此错误:not all code paths return a value?

3
int search(string [][]mat, int n, string x){
//set indexes for top right element
    for(int i = 0; i<n; i++)
    {
        for(int j = n-1; j>=0; j--)
        {
                     if ( mat[i][j] == x )
              {
                 Debug.Log(x +""+"Found at "+i +" "+j);
                // int[] n2 = new int[] {2, 4, 6, 8};
                // int [] xyz = new int [] {i, j};
                           return i;

              }

        }
    }}

如何解决这个错误:not all code paths return a value?
错误信息: *Assets/Scripts/Chess/Bishop.cs(237,22): error CS0161: `Bishop.search(string[][], int, string)': not all code paths return a value*
问题原因是该函数的所有代码路径都没有返回值。需要在函数中添加返回语句,确保所有情况下都有返回值。
4个回答

8

确定如果您从未找到x希望发生什么,并在方法结尾处返回该内容。例如:

// Fixed naming conventions and indentation...
// Why do we need n here at all? Why not just use the length of the array?
int Search(string[][] mat, int n, string x)
{
    //set indexes for top right element
    for (int i = 0; i < n; i++)
    {
        // Why are we looking backwards here?
        for (int j = n - 1; j >= 0; j--)
        {
            if (mat[i][j] == x)
            {
                // More readable formatting...
                Debug.Log(string.Format("{0} found at {1}, {2}", x, i, j));
                return i;   
            }   
        }
    }
    // Not found: return -1 to indicate failure. Or you could throw an exception
    return -1;
}

更一般地说,编译器错误信息相当清晰 - 在方法结束时有一种方法可以不返回任何内容。 值得退后一步并尝试思考为什么您不能自己解决这个问题。 您是否足够关注编译器错误信息? 您是否已经考虑过方法在所有情况下可能做的事情? 下一次您如何更好地处理此问题?

2
你需要在循环外添加一个return
思考一下:如果传入的是-1,将永远不会执行循环,因此当前存在的return将永远无法到达。如果mat数组中不包含x,则情况相同。
int search(string [][]mat, int n, string x)
{
    for(int i = 0; i<n; i++)
    {
        for(int j = n-1; j>=0; j--)
        {
            if ( mat[i][j] == x )
            {
                Debug.Log(x +""+"Found at "+i +" "+j);
                return i;
            }
        }
    }

    return -1; // <----
}

1

你的循环之外没有返回任何内容。这是因为你得到了这个错误信息。使用以下方式:

int Search(string [][]mat, int n, string x)
{
    //set indexes for top right element
    for(int i = 0; i<n; i++)
    {
        for(int j = n-1; j>=0; j--)
        {
            if (mat[i][j] == x)
            {
                Debug.Log(x +""+"Found at "+i +" "+j);
                // int[] n2 = new int[] {2, 4, 6, 8};
                // int [] xyz = new int [] {i, j};
                return i;

            }   
        }
    }
    return -1;
}

2
对我来说,返回0似乎不是一个好主意 - 这与在第一行找到它的结果相同... - Jon Skeet

0

这是因为您的代码所有可能的执行路径都没有返回值。尝试像这样从for循环中返回一些值:

int search(string [][]mat, int n, string x){
        //set indexes for top right element
            for(int i = 0; i<n; i++)
            {
                for(int j = n-1; j>=0; j--)
                {
                             if ( mat[i][j] == x )
                      {
                         Debug.Log(x +""+"Found at "+i +" "+j);
                        // int[] n2 = new int[] {2, 4, 6, 8};
                        // int [] xyz = new int [] {i, j};
                                   return i;

                      }

                }         
            }
      return -1;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接