回文数

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Demo {

   /**回文数
    */
   public static void main(String[] args) {
    String s;    
     while ( true) {    
    System.out.println( "请输入字符串");    
    Scanner reader = new Scanner(System.in);    
    s = reader.nextLine();    
     try{    
    Integer.parseInt(s);    
     break;    
    } catch(Exception e){    
    JOptionPane.showMessageDialog( null, "输入的不是一个数,请重新输入");    
    }    
    }    
     char a[] = s.toCharArray();    
     char b[] = new char[a.length];    
     int i;    
     for (i = 0; i < a.length; i++) {    
    b[a.length - i - 1] = a[i];    
    }    
    String x = new String(a);    
    String y = new String(b);    
     if (x.equals(y))    
    System.out.println( "回文数");    
     else    
    System.out.println( "非回文数");    


  }

}
代码2
public class Demo {

   /**
    * 回文数
    */
   public static void main(String[] args) {
     // TODO Auto-generated method stub

    String str= "abcba";
    String s="";
     for( int i=str.length()-1;i>=0;i--){
      s+=str.charAt(i);
    }
    System.out.println(s);
     if(str.equals(s)){
      System.out.println( "是回文数");
    }
    
  }
}

你可能感兴趣的:(回文数)