Chinaunix首页 | 论坛 | 博客
  • 博客访问: 739412
  • 博文数量: 771
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-11 14:24
文章分类

全部博文(771)

文章存档

2011年(1)

2008年(770)

我的朋友

分类:

2008-09-11 14:32:03

    package com.work.util;

    import java.io.BufferedReader;

    import java.io.InputStreamReader;

    /**

     * @author cuiwx

     * wangmj整理优化。

     *

     */

    public class IdCard {

        // 经过计算得出的指数数组,算法:2的n-1次方求和,除以11取模

        // 如:2的0次方除以11取模=1,2的1次方除以11取模=2,2的2次方除以11取模=4

        static int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

        // 校验位数组

        static char[] ai = { '1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' };

 

        public static void main(String args[]) {

            // IdCard ic = new IdCard();

            // System.currentTimeMillis();

            try {

                boolean flag = false;

                while (flag) {

                    BufferedReader reader = new BufferedReader(

                            new InputStreamReader(System.in));

                    System.out.println("15位身份证号:");

                    String lowerid = reader.readLine();

                    if (lowerid.equals("quit")) {

                        System.out.println("bye~~");

                        break;

                    }

                    System.out.println("8位出生日期(19791216):");

                    String birth = reader.readLine();

 

                    System.out.println("请输入性别");

                    String sex = reader.readLine();

 

                    System.out.println(checkIdNumber(lowerid, birth, sex));

                    System.out.println("18位号:" + upperIdNumber(lowerid, birth));

                    System.out.println(checkIdNumber(upperIdNumber(lowerid, birth),

                            birth, sex));

                }

            } catch (Exception e) {

                System.out.println(e);

            }

 

 

        }

 

        /**

         * 根据15位身份证号和出生日期计算得出18位身份证号

         *

         * @param lowerId,15位身份证号

         * @param birthday出生日期,19810912

         * @return upperId,返回18位身份证号

         */

        public static String upperIdNumber(String lowerId, String birthday) {

            if (lowerId.length() != 15) {

                return "请录入15位身份证号码。";

            } else {

                return lowerId.substring(0, 6) + birthday.substring(0, 2)

                        + lowerId.substring(6) + ai[checkBit(lowerId, birthday)];

            }

            // return lowerId+ai[checkBit("372832198109126616")];

        }

 

        /**

         * 根据15位身份证号和出生日期,计算校验位

         *

         * @param lowerId,15位身份证号

         * @param birthday出生日期,19810912

         * @return mod,第18位校验位,用于从ai数组取数作为身份证号的最后一位,即ai[mod]

         */

        public static int checkBit(String lowerId, String birthday) {

            if (lowerId.length() != 15)// 请录入15位身份证号码

                return -1;

 

            lowerId = lowerId.substring(0, 6) + birthday.substring(0, 2)

                    + lowerId.substring(6);

            int sum = 0;

            // 计算校验位,前 17位加权求和,然后除以11取模

            for (int i = 1; i < lowerId.length() + 1; i++) {

                sum = sum + wi[i - 1]

                        * (Integer.parseInt(lowerId.substring(i - 1, i)));

            }

            // System.out.println("sum = " + sum);

            // 计算校验位end

            int mod = sum % 11;

 

            return mod;

        }

 

        /**

         * 根据传入的18位身份证号,计算校验位

         *

         * @param id,18位身份证号

         * @return mod,返回校验位,用于从ai数组取数作为身份证号的最后一位,即ai[mod]

         */

        public static int checkBit(String id) {

            String lowerId = id.substring(0, 17);

            int sum = 0;

            for (int i = 1; i < lowerId.length() + 1; i++) {

                sum = sum + wi[i - 1]

                        * (Integer.parseInt(lowerId.substring(i - 1, i)));

            }

 

            int mod = sum % 11;

 

 

[1]    

【责编:landy】

--------------------next---------------------

阅读(418) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~