Java – a method that does not return a value when “return” is encountered in “if”
•
Java
My problem is that this method "islargest" encounters "return true;" Time does not end Even if the condition in "else if" is true, it seems to just skip it and always return "false" I even printed a word "test" to prove this When I use this method, it displays "test" and returns false
I tried to put this "return false" in else or other content, but then said "this method must return Boolean type" and suggested that I add a return statement What should I do?
public boolean isLargest(Node tmp,Node parent){ if(tmp.value > parent.value){ parent = parent.right; tmp.isLargest(tmp,parent); } else if(parent.value == tmp.value){ System.out.println("test"); return true; } return false;
Solution
When you have a recursive call, you need to return the value of the recursive call
public boolean isLargest(Node tmp,Node parent){ if(tmp.value > parent.value){ parent = parent.right; return tmp.isLargest(tmp,parent); } else if(parent.value == tmp.value){ System.out.println("test"); return true; } return false;
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
二维码