回文整数

回文整数是指正读和反读相同的整数,输入一个整数,判断其是否为回文整数。

  1: // 回文整数 
  2: import java.io.*;
  3: public class t2_14{
  4:   public static void main(String argv[]){
  5:     String str = null;
  6:     System.out.println("Please input your number : ");
  7:     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  8:     try {
  9:       str = in.readLine();
 10:     } catch (IOException e) {
 11:       // TODO 自动生成的 catch 块 
 12:       e.printStackTrace();
 13:     }
 14:     if(str.isEmpty()){
 15:       System.out.println("Empty ...");
 16:       System.exit(5);
 17:     }
 18:     str = str.trim();
 19:     if(str.startsWith("-"))
 20:       str = str.substring(1);
 21:     if(str.length()%2!=0){
 22:       char m = str.charAt(str.length()/2);
 23:       if(m<'0' || m>'9'){
 24:         System.out.println("It's not a number !");
 25:         System.exit(4);
 26:       }
 27:     }
 28:     int length = str.length()/2;
 29:     for(int i=0;i<length;i++){
 30:       char head = str.charAt(i);
 31:       if(head<'0' || head>'9'){
 32:         System.out.println("It's not a number !");
 33:         System.exit(1);
 34:       }
 35:       char tail = str.charAt(str.length()-i-1);
 36:       if(tail<'0' || tail>'9'){
 37:         System.out.println("It's not a number !");
 38:         System.exit(2);
 39:       }
 40:       if(head!=tail){
 41:         System.out.println("It's not a right number !");
 42:         System.exit(3);
 43:       }
 44:     }
 45:     System.out.println("It's right number !");
 46:   }
 47: }

转载于:https://my.oschina.net/showcolors/blog/72257

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值