java判斷身份證信息小程序(無圖形界面)

請輸入要查詢的身份證號碼,輸入exit退出:
53010219200508011x
身份證號:53010219200508011x
所屬地區:雲南省
出生日期:1920年5月8日
性別:男性
校驗位:正確。
------------------------------------------
請輸入要查詢的身份證號碼,輸入exit退出:
530102192005080111
身份證號:530102192005080111
所屬地區:雲南省
出生日期:1920年5月8日
性別:男性
校驗位:錯誤,校驗爲應該是:x
------------------------------------------
請輸入要查詢的身份證號碼,輸入exit退出:


package identity_card;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
* 驗證身份證
*/
public class Go {

/** 保存全國各地區身份證省份代碼 */
private Map<Integer, String> city = new HashMap<Integer, String>();

/** 主函數,不多說 */
public static void main(String[] args) {
new Go().init();
}

/** 初始化相關參數及數據 */
public void init() {
insert();
input();
}

/** 接收輸入,判定結果 */
public void input() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("請輸入要查詢的身份證號碼,輸入exit退出:");
String str = sc.nextLine();// 接收一行輸入
if (str.toString().equals("exit")) {// 判斷是否輸入的是"exit",如果不是,繼續執行,否則退出
System.out.println("系統已經退出。");
System.exit(0);// 退出
}
if (str.length() == 18) {// 身份證必須等於18位纔可以判斷,否則不判斷,這裏面判斷等於18位的
boolean status = true;// 設初始狀態爲可行的
for (int i = 0; i < str.length() - 1; i++) {// 這裏面只判斷前17位是否是數字,因爲最後一位可以是"x"或"X"
if (!Character.isDigit(str.charAt(i))) {// 如果當前字節不是數字,打印提示,改變status的值
System.out.println("你輸入的內容不符合要求。");
status = false;
}
}
/*
* 上面的循環走完了,如果沒有出錯,status還是等於true,所以這裏我們再判斷最後一位
* 如果上面出錯了,status不等於true,那麼下面的if就不執行了
*/
if (status) {// 這個地方也可以寫成if (status==true){
/*
* 前17位判斷完成後,接下來,我們判定最後一位,最後一位只能是"x"或"X"
*/
if (Character.isDigit(str.charAt(17))
|| str.charAt(17) == 'x' || str.charAt(17) == 'X') {// 如果滿足繼續執行,否則執行else
/** 能跑到這裏面,說明前面的判斷都沒有問題,現在開始拆分內容了 */
{// 這一組括號其實是沒有用的,在這是爲了區分模塊,這個模塊是顯示輸入的內容的
System.out.print("身份證號:");
System.out.println(str);
}
{// 這個模塊是用來顯示地區的
String home = city.get(new Integer(str.substring(0,
2)));// 查詢所屬城市,如果查不到爲null
System.out.print("所屬地區:");
if (home != null)
System.out.println(home);
else
System.out.println("不存在");
}
{// 這個模塊是用來顯示出生日期的
int year = Integer.valueOf(str.substring(6, 10));
int month = Integer.valueOf(str.substring(10, 12));
int day = Integer.valueOf(str.substring(12, 14));
System.out.print("出生日期:");
if (year > 0) {// 年份必須是大於0的
if (month > 0 && month <= 12) {// 月必須大於0,月小於13,日還要單獨算
if (day > 0) {// 這裏只判定大於0,小於多少,單獨判定,因爲年份月份不同,天數不同
int days = getDays(year, month);// 判斷當前年份下,當前月下有多少天,區分平閏年
if (day <= days) {
System.out.print(year + "年");
System.out.print(month + "月");
System.out.println(day + "日");
} else {
System.out.println("不存在,具體幾日有問題。");
}
} else {
System.out.println("不存在,具體幾日有問題。");
}
} else {
System.out.println("不存在,具體幾月有問題。");
}
} else {
System.out.println("不存在,具體哪一年有問題。");
}
}
{// 這個模塊是用來顯示性別的
System.out.print("性別:");
System.out.println((Integer.valueOf(str.substring(
16, 17))) % 2 == 1 ? "男性" : "女性");
}
{// 這個模塊是用來顯示校驗碼是否正確的
int last = getCheck(str);// 存放最後一位計算出來的值,用於和輸入的值進行比較
System.out.print("校驗位:");
if ((str.substring(17, 18) + "").equals(String
.valueOf(last))
|| (str.substring(17).equals("x") && last == -1)
|| (str.substring(17).equals("X") && last == -1)) {// 比較最後一位是否和計算的值一致
System.out.println("正確。");
} else {
System.out.println("錯誤,校驗爲應該是:"
+ ((last != -1) ? last : "x"));
}
}
} else {
System.out.println("你輸入的內容不符合要求。");
}
}
} else {// 這裏面判斷不等於18位的
System.out.println("你輸入的內容不符合要求。");
}
System.out.println("------------------------------------------");
}
}

/** 判斷當前年份下,當前月下有多少天,區分平閏年 */
public int getDays(int year, int month) {// 判斷當前年份下,當前月下有多少天,區分平閏年
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {// 這些月下,不管是平年,還是閏年,都是31天
return 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {// 這些月下,不管是平年,還是閏年,都是30天
return 30;
} else {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {// 這是閏年
return 29;
} else { // 這是平年
return 28;
}
}
}

/** 獲取身份證校驗碼,參數是身份證字符串,返回值-1代表的是"x"或"X" */
public int getCheck(String str) {
/** 這個變量用來存放所有校驗和 */
int sum = 0;
/** 這個數組裏面存放的是身份證校驗位係數,coefficient的縮寫 */
int[] coe = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
/** 這個數組裏面存放的是身份證校驗對應的最後一位身份證號碼 */
int[] check = { 1, 0, -1, 9, 8, 7, 6, 5, 4, 3, 2 };// -1代表的是"x"或"X"
for (int i = 0; i < str.length() - 1; i++) {
sum = sum + Integer.parseInt(str.charAt(i) + "") * coe[i];
}
return check[sum % 11];
}

/** 將全國各地區身份證省份代碼插入到private Map<Integer, String> city集合中 */
public void insert() {
city.put(11, "北京市");
city.put(12, "天津市");
city.put(13, "河北省");
city.put(14, "山西省");
city.put(15, "內蒙古自治區");
city.put(21, "遼寧省");
city.put(22, "吉林省");
city.put(23, "黑龍江省");
city.put(31, "上海市");
city.put(32, "江蘇省");
city.put(33, "浙江省");
city.put(34, "安徽省");
city.put(35, "福建省");
city.put(36, "江西省");
city.put(37, "山東省");
city.put(41, "河南省");
city.put(42, "湖北省");
city.put(43, "湖南省");
city.put(44, "廣東省");
city.put(45, "廣西壯族自治區");
city.put(46, "海南省");
city.put(5, "0重慶市");
city.put(51, "四川省");
city.put(52, "貴州省");
city.put(53, "雲南省");
city.put(54, "西藏自治區");
city.put(61, "陝西省");
city.put(62, "甘肅省");
city.put(63, "青海省");
city.put(64, "寧夏回族自治區");
city.put(65, "新疆維吾爾自治區");
city.put(71, "臺灣省(886)");
city.put(81, "香港特別行政區");
city.put(82, "澳門特別行政區");
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章