Java – the compiler says the return statement is missing, but I already have 3
•
Java
It's strange My compiler says I missed a return statement, but I already have three
public int tortoiseMoves() { int i = tGen(); if (i >= 1 && i <= 5) { int fastplod = 3; return fastplod; } if (i >= 6 && i <= 8) { int slowplod = 1; return slowplod; } if (i >= 9 && i <= 10) { int slip = -6; return slip; } }
Solution
You must ensure that a value is always returned If all your conditions fail, you won't return it
The fix will link your if statements because they are exclusive and use else to capture all other situations
public int tortoiseMoves() { int i = tGen(); if (i >= 1 && i <= 5) { int fastplod = 3; return fastplod; } else if (i >= 6 && i <= 8) { int slowplod = 1; return slowplod; } else if (i >= 9 && i <= 10) { int slip = -6; return slip; } else { // return something or throw exception return 0; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码