身份证校验
1 public class Id18 { 2 int[] weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; // 十七位数字本体码权重 3 char[] validate = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // mod11,对应校验码字符值 4 5 public char getValidateCode(String id17) { 6 int sum = 0; 7 int mode = 0; 8 for (int i = 0; i < id17.length(); i++) { 9 sum = sum + Integer.parseInt(String.valueOf(id17.charAt(i))) * weight[i]; 10 } 11 mode = sum % 11; 12 return validate[mode]; 13 } 14 15 public static void main(String[] args) { 16 Id18 test = new Id18(); 17 System.out.println("该身份证验证码:" + test.getValidateCode("23092119970215128")); // 该身份证校验码:X 18 } 19 }