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];
}
}