ID number checking program


Submit solution

Points: 20
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type

題目說明

<1101檢定考題>
因應疫情嚴峻,到賣場與超市消費都要以身分證號進行管制,然而身分證號的產生是有一個邏輯性的。
有效身分證號檢查步驟如下:

  1. 身分證共10碼,第1碼為大寫A到Z,第2~10碼為阿拉伯數字
  2. 身份證字號(連同英文字母共 10 碼)中的每碼代表的意義如下:以 A123456789 為例
    110131

  3. 接下來將第一個英文字母轉換成數字,轉換規則如下
    110132

  4. 轉換後的 2 位數字連同性別代碼和流水編號(不包含檢查碼)共有 10 個位數,將每個位數由左至右依照 ×1、×9、×8、×7、×6、×5、×4、×3、×2、×1 的順序相乘。
    110133

  5. 將上個步驟的10個乘積相加後除以10並取其餘數。再用10減去該餘數後即得檢查號碼(若餘數為 0 時,則設定其檢查碼為 0),
    若驗證碼與身份證相符則該身分證字號即為有效證號。

輸入

由鍵盤輸入身分證號,每筆輸入需先檢查是否10碼,第1碼是否為大寫字母、第2~10碼為阿拉伯數字。如有錯誤需顯示在螢幕上。

輸出

確認符合身分證格式後,再運用上述驗證公式進行檢查並輸出至螢幕。

sample input & output

輸入 輸出
樣本1 A22335674 身分證號非10碼
樣本2 m223014456 身分證號首碼有誤
樣本3 K103B76743 身分證號數字部分有誤
樣本4 C214556805 為有效的身分證字號
樣本5 F108011332 為有效的身分證字號

Comments


  • -1
    scu10156189  commented on Jan. 12, 2026, 4:03 p.m.

    給跟我一樣只會笨方法的人:

    import java.util.Scanner;

    public class ID {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
    
        // 輸入字串!注意是字串,因為有字元!
        String x = input.nextLine();
    
        // 字串長度非10
        if (x.length() != 10) {
            System.out.println("身分證號非10碼");
            System.exit(0); // 記得離開程式不然它會往下跑
        }
    
        // 檢查非大寫的字首,利用ASCII碼的原理
        if (x.charAt(0) > 'Z' || x.charAt(0) < 'A') {
            System.out.println("身分證號首碼有誤");
            System.exit(0); // 記得離開程式不然它會往下跑
        }
    
        // 檢查非數字的字元,一樣使用ASCII碼的原理
        for (int i = 1; i <= 8; i++) {
            int c = x.charAt(i);
            if (c < '0' || c > '9') {
                System.out.println("身分證號數字部分有誤");
                System.exit(0); // 記得離開程式不然它會往下跑
            }
        }
    
        // 以上都無誤才會往下跑,不然就結束程式
    
        // 以下開始分割身分證字元跟數字
    
        char z = x.charAt(0); // 第一個字是字元
    
        int a = x.charAt(1) - '0'; // 第一個字元-'0'(利用ASCII碼原理),這樣才會是它原本的數字!
        int b = x.charAt(2) - '0';
        int c = x.charAt(3) - '0';
        int d = x.charAt(4) - '0';
        int e = x.charAt(5) - '0';
        int f = x.charAt(6) - '0';
        int g = x.charAt(7) - '0';
        int h = x.charAt(8) - '0';
        int i = x.charAt(9) - '0';
    
        // 以下檢查身分證字號
        int sum = a * 8 + b * 7 + c * 6 + d * 5 + e * 4 + f * 3 + g * 2 + h;
        int y;
    
        if (z == 'A') {
            y = 10;
        } else if (z == 'B') {
            y = 11;
        } else if (z == 'C') {
            y = 12;
        } else if (z == 'D') {
            y = 13;
        } else if (z == 'E') {
            y = 14;
        } else if (z == 'F') {
            y = 15;
        } else if (z == 'G') {
            y = 16;
        } else if (z == 'H') {
            y = 17;
        } else if (z == 'I') {
            y = 34;
        } else if (z == 'J') {
            y = 18;
        } else if (z == 'K') {
            y = 19;
        } else if (z == 'L') {
            y = 20;
        } else if (z == 'M') {
            y = 21;
        } else if (z == 'N') {
            y = 22;
        } else if (z == 'O') {
            y = 35;
        } else if (z == 'P') {
            y = 23;
        } else if (z == 'Q') {
            y = 24;
        } else if (z == 'R') {
            y = 25;
        } else if (z == 'S') {
            y = 26;
        } else if (z == 'T') {
            y = 27;
        } else if (z == 'U') {
            y = 28;
        } else if (z == 'V') {
            y = 29;
        } else if (z == 'W') {
            y = 32;
        } else if (z == 'X') {
            y = 30;
        } else if (z == 'Y') {
            y = 31;
        } else if (z == 'Z') {
            y = 33;
        } else {
            y = 0; // y一定要賦值,不然會出現error(因為Java物件導向,可以試試看這行拿掉會怎樣)
        }
    
        int aa = y / 10; // 取y的十位數
        int bb = y % 10; // 取y的個位數
    
        // 以下檢查邏輯而已
        int cc = aa * 1 + aa * 9;
        int check = (cc + sum) % 10;
        if (i != check) {
            System.out.println("為無效的身分證字號");
        } else if (i == check) {
            System.out.println("為有效的身分證字號");
        }
    }

    }


  • 0
    scu11156105  commented on Jan. 10, 2026, 5:25 p.m.

    /


  • -2
    scu10156181  commented on June 21, 2024, 1:41 p.m.

    第五筆測資有誤 曾經通過的程式碼現在無法通過


  • 0
    scu10156153  commented on Jan. 11, 2024, 11:26 a.m. edit 3

    給不會的人做參考,我花了超多時間在改 import java.util.Scanner;

    public class test_3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
    
        String idNumber = scanner.nextLine();
    
        // 檢查身分證格式
        if (!isValidFormat(idNumber)) {
            System.exit(0);
            return;
        }
    
        // 檢查身分證號是否有效
        if (validateIdNumber(idNumber)) {
            System.out.println("為有效的身分證字號");
        } else {
            System.out.println("為無效的身分證字號");
        }
    }
    
    // 檢查身分證格式是否正確
    public static boolean isValidFormat(String idNumber) {
        if (idNumber.length() != 10) {
            System.out.println("身分證號非10碼");
            return false;
        }
    
        char firstChar = idNumber.charAt(0);
        if (!Character.isUpperCase(firstChar) || !Character.isLetter(firstChar)) {
            System.out.println("身分證號首碼有誤");
            return false;
        }
    
        for (int i = 1; i < idNumber.length(); i++) {
            if (!Character.isDigit(idNumber.charAt(i))) {
                System.out.println("身分證號數字部分有誤");
                return false;
            }
        }
    
        return true;
    }
    
    // 驗證身分證號是否有效
    public static boolean validateIdNumber(String idNumber) {
        char firstChar = idNumber.charAt(0);
        int firstDigit = charToNumber(firstChar);
    
        // 檢查邏輯
        int[] weights = { 8, 7, 6, 5, 4, 3, 2, 1 };
    
        int sum = firstDigit / 10 * 1 + firstDigit % 10 * 9; // 將轉換後的數字加入總和
    
        for (int i = 1; i < idNumber.length() - 1; i++) {
            int digit = Character.getNumericValue(idNumber.charAt(i));
            sum += digit * weights[i-1];
        }
    
        int remainder = sum % 10;
        int checkCode = (remainder == 0) ? 0 : 10 - remainder;
    
        // 檢查驗證碼是否與身份證相符
        return checkCode == Character.getNumericValue(idNumber.charAt(9));
    }
    
    private static int charToNumber(char c) {
        c = Character.toUpperCase(c);
        if (c >= 'A' && c <= 'H') {
            return c - 'A' + 10;
        } else if (c >= 'J' && c <= 'N') {
            return c - 'J' + 18; // P is mapped to 23, Q to 24, and so on
        } else if (c >= 'P' && c <= 'Z') {
            return c - 'P' + 23; // P is mapped to 23, Q to 24, and so on
        } else if (c == 'I') {
            return 34;
        } else if (c == 'O') {
            return 35;
        } else {
            return -1; // Invalid character
        }
    }

    }