Java 中缺少 return 语句错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18788204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing return statement error in Java
提问by user2776699
I am currently writing a palindrome tester in Java for a class I am taking in high school. I have asked my teacher for assistance and he is also confused as well. I was hoping the community on stackoverflow could help me out. Thank you.
我目前正在为我在高中上的一门课用 Java 编写一个回文测试器。我向我的老师寻求帮助,他也很困惑。我希望 stackoverflow 上的社区可以帮助我。谢谢你。
public class Palindrome
{
private String sentence;
public Palindrome(String s)
{
sentence = s;
}
public boolean isPalindrome()
{
if(sentence.length() <= 1)
{
return true;
}
if(sentence.charAt(0) == sentence.charAt(sentence.length()-1))
{
sentence = sentence.substring(1, sentence.length()-1);
isPalindrome();
}
else
return false;
}
}
回答by Eric Stein
You need return isPalindrome();
. Otherwise the method isn't returning anything in that case, and it's declared to return a boolean.
你需要return isPalindrome();
. 否则,在这种情况下,该方法不会返回任何内容,并且它被声明为返回一个布尔值。
回答by Konstantin Yovkov
Change
改变
if (sentence.charAt(0) == sentence.charAt(sentence.length()-1))
{
sentence = sentence.substring(1, sentence.length()-1);
isPalindrome();
}
to
到
if (sentence.charAt(0) == sentence.charAt(sentence.length()-1))
{
sentence = sentence.substring(1, sentence.length()-1);
return isPalindrome();
}
In order to have the method complied, JVM need to make sure the method has a return statement for every possible case (which is something you haven't done).
为了使该方法得到遵守,JVM 需要确保该方法对每种可能的情况都有一个 return 语句(这是您尚未完成的事情)。
回答by gia
your last if clause misses a return, if you really want to return false in the else-clause.
如果您真的想在 else 子句中返回 false,那么您的最后一个 if 子句会错过返回值。
回答by Jim Rhodes
If your code takes this path there is no return statement. If your teacher is confused, you need a new teacher.
如果您的代码采用此路径,则没有 return 语句。如果您的老师感到困惑,您需要一位新老师。
if(sentence.charAt(0) == sentence.charAt(sentence.length()-1))
{
sentence = sentence.substring(1, sentence.length()-1);
isPalindrome();
}
回答by Fred
You want to use a recursive way to check if the sentence is palindrome. you'd better to return isPalindrome() method inside following snippet
您想使用递归方式检查句子是否为回文。您最好在以下代码段中返回 isPalindrome() 方法
if(sentence.charAt(0) == sentence.charAt(sentence.length()-1))
{
sentence = sentence.substring(1, sentence.length()-1);
return isPalindrome();
}
This is my code:
这是我的代码:
public class palindrome
{
private String sentence;
public palindrome(String s)
{
sentence = s;
}
public boolean isPalindrome()
{
if(sentence.length() <= 1)
{
return true;
}
if(sentence.charAt(0) == sentence.charAt(sentence.length()-1))
{
sentence = sentence.substring(1, sentence.length()-1);
return isPalindrome();
}
else
return false;
}
public static void main(String [] args)
{
palindrome p=new palindrome("aabbaa");
if(p.isPalindrome())
System.out.println("yes");
else
System.out.println("no");
}
}