当我尝试运行这个程序时,发生无限循环。我无法确定问题出在主要方法还是递归方法中。回文递归 - 无限循环
这里是递归方法。
public class RecursivePalindrome
{
public boolean isPalindrome(String s)
{
if(s.length() <= 1)
{
return true;
}
else if(s.charAt(0) == s.charAt(s.length() - 1))
{
return isPalindrome(s.substring(1,s.length() - 1));
}
else
{
return false;
}
}
}
这里是主要的方法。
public class RecursivePalindromeTester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a word or phrase. Type Q to quit: ");
String word = in.next();
RecursivePalindrome object = new RecursivePalindrome();
while(!word.equalsIgnoreCase("Q"))
{
if(object.isPalindrome(word))
{
System.out.println(word + " is a palindrome");
}
else
{
System.out.println(word + " is not a palindrome");
}
}
System.out.print("Enter another word or phrase. Type Q to quit: ");
word = in.next();
}
}
@lurker'endIndex'是唯一的。 – user2336315