Java简单的身份证枚举器

JAVA简单的身份证后四位枚举


/**
 * @author nopromise
 */
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Main {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);
                String id = null;
                System.out.print("请输入身份证前14位:");
                String befor = sc.next();
                if (befor.length() != 14) {
                        System.out.println("格式错误!!!");
                        System.exit(1);
                }
                System.out.println("0.女 \r\n1.男");
                System.out.print("请输入序号:");
                int sex = sc.nextInt(), cache = sex;
                if (sex != 1 && sex != 0) {
                        System.out.println("格式错误!!!");
                        System.exit(1);
                }
                int a = 0, b = 0, num = 0;
                while (true) {
                        id = befor + a + b + sex;
                        try {
                                write(id + checkCode(id));

                                System.out.println("写入:" + id + checkCode(id));
                                num++;
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                        if (a == 9 && b == 9) {
                                if (sex == 8 || sex == 9) {
                                        System.out.println("done!!! sum:" + num + "行");
                                        break;
                                } else {
                                        sex += 2;
                                        continue;
                                }

                        }
                        if (sex == 8 || sex == 9) {
                                b ++;
//                                重置
                                sex = cache;

                        } else if (b == 9){
                                b = 0;
                                a++;
                        } else {
                                sex += 2;
                        }
                }

        }
//         写入txt
        public static void write(String s) throws Exception {
                //                创建文件写入并处理异常
                try {
                        FileWriter writeText = new FileWriter("D:\\software\\OneDrive\\桌面\\identity.txt", true);
                        writeText.write(s + "\r\n");
                        writeText.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

//        校验码
        public static char checkCode(String id)
        {
                char pszSrc[]=id.toCharArray();
                int iS = 0;
                int iW[]={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                char szVerCode[] = new char[]{'1','0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
                int i;
                for(i=0;i<17;i++)
                {
                        iS += (int)(pszSrc[i]-'0') * iW[i];
                }
                int iY = iS%11;
                return szVerCode[iY];
        }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值