求Java大神给个代码!身份证第18位计算法

1个回答

  • import java.util.Scanner;

    public class IDValidator {

    private final int[] xishu = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7,

    9, 10, 5, 8, 4, 2 };

    private final char[] last = new char[] { '1', '0', 'X', '9', '8', '7', '6',

    '5', '4', '3', '2' };

    private String id;

    public IDValidator(String id) {

    this.id = id;

    }

    public boolean validate() {

    if (null == id || id.length() != 18) {

    return false;

    }

    char[] string = id.toCharArray();

    int sum = 0;

    for (int i = 0; i < string.length - 1; i++) {

    sum += (string[i] - '0') * xishu[i];

    }

    return last[sum % 11] == string[17];

    }

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    String id = in.next();

    IDValidator validator = new IDValidator(id);

    System.out.println(validator.validate());

    }

    }