-1

プログラミングを勉強中の素人です。

私は回文をしようとしています。ただし、コードにエラーがあります。

public class Palindrome {

    public static void main(String args[])
    {
    String pal = "abc";

    public static void check(String pal)
    {
        if(pal==null)
        {
            System.out.println("Null Value..Exit");
        }
        else
        {
            StringBuilder str= new StringBuilder(pal);
            str.reverse();
            System.out.println(str.reverse());
        }
    }
    }

}

どこが間違っていますか?申し訳ありませんが、私はプログラミングに非常に慣れていません。ただ学ぼうとしているだけです!

4

3 に答える 3

3

コードに以下の変更を加える必要があります。

public static void main(String args[]) {
    String pal = "abc";
    check(pal); // Nested methods are not allowed, thus calling the check
                // method, which is now placed outside main
}

public static void check(String pal) {
    if (pal == null) {
        System.out.println("Null Value..Exit");
    } else {
        StringBuilder str = new StringBuilder(pal);

        // I think I confused you by doing the below.
        // str = str.reverse(); // commenting this

        str.reverse(); // adding this 
        // str.reverse(); reverse will affect the str object. I just assigned it back to make it easier for you
        // That's why if you add a SOP with str.reverse, it'll reverse the string again, which will give the original string back
        // Original string will always be equal to itself. that's why your if will always be true
        // give a SOP with str.toString, not reverse.

        // str.toString is used because str is a StringBuilder object and not a String object.
        if (pal.equals(str.toString())) { // if the string and its reverse are equal, then its a palindrome
            System.out.println("Palindrome");
        } else {
            System.out.println("Not a Palindrome");
        }
    }
}
于 2013-10-05T07:19:45.733 に答える
2

あるメソッドを別のメソッド内に記述することはできません。

public class Palindrome {

    public static void main(String args[])
    {
    String pal = "abc";
check(pal);

    }
    public static void check(String pal)
    {
        if(pal==null)
        {
            System.out.println("Null Value..Exit");
        }
        else
        {
            StringBuilder str= new StringBuilder(pal);
            str.reverse();
            System.out.println(str.reverse());
        }
    }
}
于 2013-10-05T07:13:43.550 に答える