【问题标题】:Missing return statement error in JavaJava中缺少返回语句错误
【发布时间】:2013-09-18 06:04:08
【问题描述】:

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

}

【问题讨论】:

  • 这是一种非常低效的检查回文的方法(因为每个substring 调用都会创建一个新字符串),这有关系吗?对字符串进行简单的 for 循环会好得多。

标签: java return palindrome


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    改变

    if (sentence.charAt(0) == sentence.charAt(sentence.length()-1))
    {
        sentence = sentence.substring(1, sentence.length()-1);
        isPalindrome();
    }
    

    if (sentence.charAt(0) == sentence.charAt(sentence.length()-1))
    {
        sentence = sentence.substring(1, sentence.length()-1);
        return isPalindrome();
    }
    

    为了使方法符合要求,JVM 需要确保该方法对每种可能的情况都有一个 return 语句(这是你没有做过的事情)。

    【讨论】:

      【解决方案3】:

      如果你真的想在 else 子句中返回 false,你的最后一个 if 子句会错过返回值。

      【讨论】:

      • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
      • @LucasEduardo,为什么不提供答案?
      【解决方案4】:

      如果您的代码采用此路径,则没有返回语句。如果您的老师感到困惑,您需要一位新老师。

      if(sentence.charAt(0) == sentence.charAt(sentence.length()-1))
      {
          sentence = sentence.substring(1, sentence.length()-1);
          isPalindrome();
      }
      

      【讨论】:

        【解决方案5】:

        您想使用递归方式来检查句子是否为回文。你最好在下面的 sn-p 中返回 isPalindrome() 方法

        if(sentence.charAt(0) == sentence.charAt(sentence.length()-1))
                {
                    sentence = sentence.substring(1, sentence.length()-1);
                    return isPalindrome();
                }
        

        这是我的代码:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-09
          • 2014-08-20
          • 2013-04-27
          • 2012-11-19
          • 1970-01-01
          • 2015-06-01
          • 1970-01-01
          相关资源
          最近更新 更多