回文数

来源:互联网 发布:做淘宝客怎么赚钱 编辑:程序博客网 时间:2024/04/30 18:46

import java.util.*;

public class NumOfHuiWen {
 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
     String s = null;
  while (sc.hasNext()) {
      try {
        s = sc.nextLine();
        if(s.length()>100) {        
         System.out.println("illegal inform!");
         return;
        } else if(!s.equals("0")) {
         System.out.println(computeNum(s));
        } else {
      sc.close();
      System.exit(-1);
      return;
        }
      } catch(Exception e) {
       e.printStackTrace();
      }

  }

 }
 public static String computeNum(String s) {
  int slength = s.length();
  if(slength<1) {
   return "Yes";
  }
  int left = 0;
  int right = s.length()-1;
  while(left < right) {
   if(s.charAt(left++)!=s.charAt(right--))
    return "No";
  }
  return "Yes";  
 }
}

原创粉丝点击