Java中缺少返回语句错误

2022-01-19 00:00:00 return java palindrome

我目前正在为我在高中上的一门课用 Java 编写回文测试器.我向我的老师寻求帮助,他也很困惑.我希望stackoverflow上的社区可以帮助我.谢谢.

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.

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;
    }

}

推荐答案

你需要return isPalindrome();.否则,该方法在这种情况下不会返回任何内容,并且它被声明为返回布尔值.

You need return isPalindrome();. Otherwise the method isn't returning anything in that case, and it's declared to return a boolean.

相关文章