你好,我試圖做一個程序,檢查一個單詞的第一個字母,看它是否等於最後一個字母。我的java編譯正常,但什麼都沒顯示
public class isPalindrome
{
public static void main(String[] args)
{
// Create an array of strings to test.
String[] testStrings = { "Able was I ere I saw Elba",
"Rats live on no evil star",
"Four score and seven years ago",
"Barrack Obama",
"Now is the time for all good men",
"Desserts I stressed",
"Ask not what your country can do for you",
"Kayak",
"Vegeta",
"A Man, A Plan, a canal, Panama!"};
for (int i = 0; i < testStrings.length; i++)
{
System.out.print("\"" + testStrings[i] + "\"");
if (Palindrome(stripString(testStrings[i])))
System.out.println(" is a palindrome.");
else
System.out.println(" is not a palindrome.");
}
}
public static String stripString(String strip)
{
strip = strip.toUpperCase();
String stripped= "";
for (int i= 0; i< strip.length(); i++)
{
if (Character.isLetter(strip.charAt(i)))
stripped += strip.charAt(i);
}
return stripped;
}
public static boolean Palindrome(String str)
{
boolean status = false;
if (str.length() <= 1)
status = true;
else if (str.charAt(0) == str.charAt(str.length()-1))//Recursive Case
{
status = Palindrome (str.substring (1, str.length()-1));
}
return status;
}
}
真的什麼都沒有?至少從for循環的東西應該打印... – BoltClock 2011-03-02 08:05:04
這看起來很好,我的程序終止?還是它在某個地方陷入了無限循環? – Argote 2011-03-02 08:06:56
這就是我在想什麼? – lonesarah 2011-03-02 08:07:06