不明白为什么我在别的地方收到这个错误。

时间:2015-03-13 03:15:34

标签: if-statement recursion methods palindrome

public static boolean isPalindrome(String word, int firstIndex, int lastIndex)
{
    if(firstIndex>lastIndex)
        return true;    
    else if(word.charAt(firstIndex)==(word.charAt(lastIndex)));
    {
        return true && isPalindrome(word, firstIndex+1, lastIndex-1);   
    }
    **else**
        return false;

}

在else上获取错误:“令牌上的语法错误”else“,删除此令牌 “ 我真的不知道这个代码有什么问题,特别是那个else语句。

2 个答案:

答案 0 :(得分:1)

如果

,删除else末尾的半冒号
else if(word.charAt(firstIndex)==(word.charAt(lastIndex)));

答案 1 :(得分:0)

试试这个:

    public static boolean isPalindrome(String word, int firstIndex, int lastIndex)
{  
     boolean palindrome = false;
    if(firstIndex>lastIndex) {
          palindrome = true;   
     } 
    else if(word.charAt(firstIndex)==(word.charAt(lastIndex)))
    {
        if (isPalindrome(word, firstIndex+1, lastIndex-1)) {
           palindrome = true;
       }  
    }

      return palindrome;

}
相关问题