您好我正在尝试创建一个检查单词首字母的程序,看看它是否等于最后一个字母。
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;
}
}
答案 0 :(得分:3)
我的java编译得很好但没有显示任何内容。
是的确如此:
#~/temp$ javac isPalindrome.java #~/temp$ java isPalindrome "Able was I ere I saw Elba" is a palindrome. "Rats live on no evil star" is a palindrome. "Four score and seven years ago" is not a palindrome. "Barrack Obama" is not a palindrome. "Now is the time for all good men" is not a palindrome. "Desserts I stressed" is a palindrome. "Ask not what your country can do for you" is not a palindrome. "Kayak" is a palindrome. "Vegeta" is not a palindrome. "A Man, A Plan, a canal, Panama!" is a palindrome.
编译后你是不是在运行它?这个命令编译它:
javac isPalindrome.java
然后这个运行它:
java isPalindrome
修改:或者您可能需要:
java -cp . isPalindrome
...告诉Java在当前目录中查找类。 (我不是在我很大程度上没有定制的Java安装,但你曾经不得不这样做,所以......)
偏离主题:这是没有与问题有关,但是:在Java中,编码约定(您可以自由忽略)就是那个类名称以大写字母开头,方法名称以小写字母开头。因此,该类将为IsPalindrome
(或PalindromeTester
或其他内容),然后方法Palindrome
将为isPalindrome
或类似。如果你使用这个约定,那么与其他人一起工作会更容易,因为你不会把它们与不寻常的事物命名混淆。 : - )