-2
// The "PalinDrome" class.
import java.awt.*;
import hsa.Console;

public class PalinDrome
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        c.println("Please enter a word");
           String word = c.readLine ();
        int i;
        int num = word.length ();
        String str = "";
        for (i = num - 1 ; i >= 0 ; i--)
            str = str + word.charAt (i);
        if (str.equals (word))
            c.println (word + " is a palindrome");
        else
            c.println (word + " is not a palindrome");




        // Place your program here.  'c' is the output console
    } // main method
} // PalinDrome class

I have created a palindrome program for my exam project. The program works fine with lower cased letters such as "mom" but will not work when there is a capital letter such as "Mom". Do you have any suggestions on what I can do?

4

4 回答 4

1

使用String#equalsIgnoreCase而不是equals方法,它忽略了大小写考虑。

if (str.equalsIgnoreCase(word)){
  ...
}else
  ...
于 2013-01-23T09:11:08.387 回答
1

改变这个

if (str.equals (word))

if (str.equalsIgnoreCase(word))

忽略特定情况进行字符串比较。

于 2013-01-23T09:12:45.090 回答
0

读取word变量后,修改为小写: word = word.toLowerCase()

其余的可以保持不变,它会工作。

于 2013-01-23T09:14:57.490 回答
0

在检查它是否为 Palidrome 之前,只需使用字符串的 toUppercase (或 toLowerCase)

于 2013-01-23T09:11:51.600 回答