Missing return statement in for loop

后端 未结 3 1624
野的像风
野的像风 2021-01-15 04:37

I have a for loop that returns values from an array.

public static String getPieces(){
        for(int y=0; y<=7; y++)
            for(int x         


        
3条回答
  •  终归单人心
    2021-01-15 05:20

    because return statement is under for block.

    As you have mentioned return type is String and your method has to have return tye. But with your program compiler does not know in advance that flow does not go under for loop and no return is possible. So compiler is extra smart. Is n't it :)

    Try

    public static String getPieces(){
           String result = null;
            for(int y=0; y<=7; y++)
                for(int x=0; x<=7; x++)
                    result= piece[x][y];
    
            return result;
    }
    

    it will work

提交回复
热议问题