我想检查一个单词是否是回文,我正在使用递归,我不知道我在做什么错,但是当我到达基本情况时,该方法一直调用最终所有字返回错误。任何人都可以帮我找到错误吗?谢谢:/返回语句不停止代码java
public static void main(String[] args)
{
System.out.print("Enter a word: ");
Scanner sc = new Scanner(System.in);
String isPalindrome = sc.next();
String regex = "[.!? ]";
isPalindrome.split(regex);
if(testPalindrome(isPalindrome)==true)
{
System.out.print(isPalindrome+" is a palindrome.");
}
else
{
System.out.print(isPalindrome+" is not a palindrome.");
}
}
public static boolean testPalindrome(String word)
{
if(word.charAt(0)==word.charAt(word.length()-1))
{
if(word.length()==1)
{
return true;
}
word = word.substring(1, (word.length()-1));
testPalindrome(word);
}
return false;
}
:| |我怎么错过了? :/非常感谢:D – noobProgrammer
现在假设答案更准确。 –