Java中缺少返回语句错误

Missing return statement error in Java(Java中缺少返回语句错误)

本文介绍了Java中缺少返回语句错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我在高中上的一门课用 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.

这篇关于Java中缺少返回语句错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java中缺少返回语句错误

基础教程推荐