How to get Rid of this error: not all code paths return a value?

血红的双手。 提交于 2021-01-27 17:01:34

问题


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;

              }

        }
    }}

How to get Rid of this error: not all code paths return a value?

Error: *Assets/Scripts/Chess/Bishop.cs(237,22): error CS0161: `Bishop.search(string[][], int, string)': not all code paths return a value*


回答1:


Work out what you want to happen if you never find x, and return that at the end of the method. For example:

// 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;
}

More generally: the compiler error message was reasonably clear here - there was a way that you could get to the end of the method without returning anything. It's worth taking a step back and trying to think about why you couldn't work this out yourself. Did you pay enough attention to the compiler error message? Had you though through everything the method might do, in all situations? How could you handle this better next time?




回答2:


You need to add a return outside of the loop.
Think about it: What if you would pass in -1 for n? The loop would never execute, hence the currently existing return would never be reached. The same happens if the mat array doesn't contain 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; // <----
}



回答3:


You not returning anything outside of your loop. This is because you getting this error message. Use it like this;

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;
}



回答4:


this happens because all possible execution path of your code not returns a value.try returning some value out of for loop like this :

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;
    }


来源:https://stackoverflow.com/questions/13760905/how-to-get-rid-of-this-error-not-all-code-paths-return-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!