判断是否回文,帮忙看看哪里出错了?
下面是我的回文程序,麻烦大家帮我看看哪里出错了?
我输入不是回文的话,正常显示不是回文,但是如果我输入的是回文如abccba的话,就出问题阿。下面是我的class和test
=============Palindrome.java====================
public class Palindrome
{
static private String text;
static private int firstIndex;
static private int lastIndex;
public static boolean isPalindrome(String text, int firstIndex,int lastIndex)
{
text = text.toLowerCase();
firstIndex = 0;
lastIndex = text.length()-1;
while( !Character.isLetterOrDigit( text.charAt( firstIndex ) ))
firstIndex++;
while( !Character.isLetterOrDigit( text.charAt( lastIndex ) ))
lastIndex++;
if (firstIndex > = lastIndex)
return true; // Base Case
// The first and last letters are different,it is not a Palindrome
else if (text.charAt(firstIndex) != text.charAt(lastIndex))
return false;
else
// recursive case
{
return isPalindrome(text, firstIndex +1, lastIndex -1);
}
}
}
=================PalindromeTest.java====================
import javax.swing.JOptionPane;
public class PalindromeTest
{
public static void main( String [] args)
{
String input = new String( " ");
String output1 = "Programmed by Yuanyuan Li\n ";
do
{
input = JOptionPane.showInputDialog( "Please enter a string or quit to exit ");
if(Palindrome.isPalindrome(input,0,input.length()-1))
JOptionPane.showMessageDialog(null,output1 + input + "is a Palindrome ");
else
JOptionPane.showMessageDialog(null,output1 + input + " is not a Palindrome ");
} while( !(input.equalsIgnoreCase( "Quit ")));