不用reverse方法,对任意一个字符串,如何判断类似:123454321 等回文字符串问题

解决方案 »

  1.   

    我是这样写的,可是不对啊?好象是算法有问题!!!请指正一下,谢谢!!
    import javax.swing.*;public class ReverseTest
    {
    public static void main(String[] args)
    {


    String input = JOptionPane.showInputDialog(null,"Please input a string","Input",JOptionPane.PLAIN_MESSAGE); int len = input.length();
    char[] ch = new char[len] ;

    input.getChars(0,len,ch,0);

    for(int i = 0,j = len - 1; i < len; i++,j--)
    {
    if(ch[i] != ch[j])
    {
    System.out.println("Not similar");
    break;
    }
    }
    System.out.println("Similar");
    }
    }
      

  2.   

    可以啊,不过循环条件和输出改了下
    import javax.swing.*;public class ReverseTest
    {
    public static void main(String[] args)
    {


    String input = JOptionPane.showInputDialog(null,"Please input a string","Input",JOptionPane.PLAIN_MESSAGE); int len = input.length();

    System.out.println(input);

    char[] ch = new char[len] ;

    input.getChars(0,len,ch,0);

    for(int i = 0,j = len - 1; i < len/2; i++,j--)
    {
    if(ch[i] != ch[j])
    {
    System.out.print("Not ");
    break;
    }
    }
    System.out.println("Similar");
    }
    }