//ID.java --- by TA import java.io.*; class ID { public static void main(String[] args) throws java.io.IOException { while (true) { System.out.print("請輸入身分證字號,輸入QUIT離開:"); String ID = input(); if (ID.compareTo("QUIT") == 0) { break; } if (!format(ID)) { error(); continue; } if (test(ID) == ID.charAt(9) - '0') { System.out.println(ID + "是合法的身分證字號。"); } else { System.out.println(ID + "不是一個合法的身分證字號。"); } } System.out.println("Bye!"); } static int area(char n) { int y[] = { 10,11,12,13,14,15,16,17, 34, 18,19,20,21,22,35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33 }; int x = (int) n; x = x - 'A'; return y[x] / 10 + (y[x] % 10) * 9; } //檢查格式 static boolean format(String ID) { if (ID.length() != 10) return false; if (ID.charAt(0) < 'A' || ID.charAt(0) > 'Z') return false; if (ID.charAt(1) != '1' && ID.charAt(1) != '2') return false; for (int i = 2; i <= 9; ++i) { if (ID.charAt(i) < '0' || ID.charAt(i) > '9') return false; } return true; } static int test(String ID) { //英文字母換成數字 int n = area(ID.charAt(0)); //計算公式 for (int i = 1; i <= 8; ++i) { n += (ID.charAt(i) - '0') * (9 - i); } return (10 - n % 10) % 10; } static String input() throws java.io.IOException { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); return buf.readLine().toUpperCase(); } static void error() { System.out.println("輸入格式錯誤。"); } }//class ID