Missing return statement with for loop

2020-05-02 00:43发布

问题:

I have a two-dimensional boolean array 'poorSignal' and need to write a method that returns a grid, where if a point on the array is true an X is displayed, if false a O is displayed. Here is my code:

    public String display()
{
    for(int i = 0; i < mapSize; i++)
    {
        for(int j = 0; j < mapSize; j++)
        {
            if(poorSignal[i][j] = true)
            {
                return "O ";
            }
            else
            {
                return "X ";
            }
        }
        return "\n";
    }
}

When I compile, it gives me 'missing return statement' on the very last line of the method. I am also unsure if the 'return "\n" will work to add a new line when printing the array.

It's a question for an assignment, so I can't print it directly or just print the boolean values - it must be a method that produces the grid.

回答1:

The compiler cannot know whether the loop is actually run, therefore you must also have a return statement outside the outer loop.

But then, if I look at your code, I am not sure whether returning is what you really want there. If it is your intention to print the entire matrix, you might want to use a StringBuilder and then use the append method inside the loops. After the outer loop, return a string representation of the builder using the toString method like so:

StringBuilder sb = new StringBuilder();
// loop
sb.append(someValue); 
sb.append('\n');
// after loop
return sb.toString();


回答2:

Seems to me that you don't want to return value for each call. Better would be creating a StringBuilder and constructing a grid inside the StringBuilder (with newLine after each outer iteration) and then returning toString of the constructed StringBuilder outside the loops.

Anyway, if your mapSize is 0, you will never get to any return statement (and as it's runtime data, compiler has no way of knowing the argument size.

public String display(){
  StringBuilder sb = new StringBuilder();
  for(int i = 0; i < mapSize; i++){
    for(int j = 0; j < mapSize; j++){
        if(poorSignal[i][j]){ //don't use comparing to true if you have stored boolean values
            sb.append("O ");
        }else{
            sb.append("X ");
        }
    }
    sb.append("\n");
  }
  return sb.toString;
}


回答3:

When the function reaches the first return, it will just return that value and quit. You should do this:

public String display(int i, int j) {
    if(poorSignal[i][j]) {
        return "O \n";
    }
    return "X \n";
}

And call it like this:

for(int i = 0; i < mapSize; i++) {
    for(int j = 0; j < mapSize; j++) {
        someStringVariable[i][j] = display(i, j);
    }
}

This way, when poorSignal[i][j] is true, it will return "O \n" and stop executing the rest of the funtion. If it is false, it will perform the other return instead. Also note "poorSignal[i][j]" instead of "poorSignal[i][j] = true", which will actually assign true to poorSignal[i][j] and return true if it succeeds, which it probably always will. You could write "poorSignal[i][j] == true", but poorSignal is already a boolean value, so the "== true" part doesn't add anything interesting.



标签: java arrays