-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

試験プロジェクト用に回文プログラムを作成しました。このプログラムは、"mom" などの小文字では問題なく動作しますが、"Mom" などの大文字がある場合は動作しません。私にできることについて何か提案はありますか?

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

パリドロームかどうかを確認する前に、文字列の toUppercase (または toLowerCase) を使用するだけです

于 2013-01-23T09:11:51.600 に答える