检验身份证号码真伪算法

</pre><pre name="code" class="java">import java.util.Scanner;

/*检验身份证号码真伪算法:
17位加权因子:7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 
检验码:{ "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }
算法:身份证前17位号码每位和相应位加权因子相乘,然后累加;
得数MOD 11后的值就是校验码的索引值;
用单例模式实现系统校验号码真伪的工具类 ; 
*/
public class SingletonDemo {

 private static SingletonDemo st;
 private static int []factors = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
 private static String []checkcode={ "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
 
 private SingletonDemo()
 {
  
 }
 
 public static SingletonDemo getInstance()
 {
  if(st == null)
   st = new SingletonDemo();
  return st;
 }
 
 
 public static boolean CheckID(String ID)
 {
  int sum = 0;
  for(int i = 0;i < 17;i ++)
  {
   int Int = Integer.parseInt(ID.substring(i, i+1));
   sum += Int*factors[i];
  }
  int temp = sum%11;
  
  if(checkcode[temp].equals(ID.substring(ID.length()-1, ID.length())))
  {
   return true;
  }
   
  return false;
 }
}

class Test5
{

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);
  String ID = sc.nextLine();
  boolean a = SingletonDemo.getInstance().CheckID(ID);
  System.out.println(a);
 // CheckID(ID);
 }
 

 
}


posted @ 2015-03-23 11:11  TobeFrank  阅读(567)  评论(0编辑  收藏  举报