话不多说,直接贴代码!!!
public void checkCert(String certno){
int sum = 0;
int[] w = new int[17];
char checkbit = new char[12];
//加权因子
w[0] = 7;
w[1] = 9;
w[2] = 10;
w[3] = 5;
w[4] = 8;
w[5] = 4;
w[6] = 2;
w[7] = 1;
w[8] = 6;
w[9] = 3;
w[10] = 7;
w[11] = 9;
w[12] = 10;
w[13] = 5;
w[14] = 8;
w[15] = 4;
w[16] = 2;
//校验码
checkbit[0] = '1';
checkbit[1] = '0';
checkbit[2] = 'X';
checkbit[3] = '9';
checkbit[4] = '8';
checkbit[5] = '7';
checkbit[6] = '6';
checkbit[7] = '5';
checkbit[8] = '4';
checkbit[9] = '3';
checkbit[10] = '2';
checkbit[11] = '\0';
if(certno.length != 18){
error("身份证应为18位");
}
char[] number = certno.toCharArray();
for(int i=0; i<17; i++){
if(number[i] < '9' || number[i] > '0'){
error("身份证不合法,前17位为数字")
}
}
for( int j=0; j<17; j++){
sum = sum + (number[j]-48)*w[j];
}
int a = sum % 11;
if(number[17] != checkbit[a]){
error("身份证不合法");
}
}