private static int x=0, y=0;//轉換英文代表的數字
public idTest(){
}
public static void main(String[] args) throws IOException {
String id;
char[] idChar;
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("請輸入身分證字號:");
id = buf.readLine();
if(id.length() != 10){
System.out.println("id長度錯誤。");
System.exit(0);
}
idChar = id.toUpperCase().toCharArray();//大寫+字元陣列
//英文轉代表數字
fistChar(idChar);
//後九碼數字轉換
int[] idInt = charToInteger(idChar);
//驗證
int sum = 0;
sum += x*1;
sum += y*9;
int j = 8;
for(int i = 1; i < idChar.length; i++){
sum += idInt[i]*j;
j--;
}
sum %= 10;
String test = ((10-sum) == idInt[idInt.length-1]) ? "正確" : "錯誤";
System.out.println("身分證" +id.toUpperCase()+ "是" +test+"的" );
}
public static String fistChar(char[] num){
switch(num[0]){
case 'A':
x = 1;
y = 0;
break;
case 'B':
x = 1;
y = 1;
break;
case 'C':
x = 1;
y = 2;
break;
case 'D':
x = 1;
y = 3;
break;
case 'E':
x = 1;
y = 4;
break;
case 'F':
x = 1;
y = 5;
break;
case 'G':
x = 1;
y = 6;
break;
case 'H':
x = 1;
y = 7;
break;
case 'I':
x = 3;
y = 4;
break;
case 'J':
x = 1;
y = 8;
break;
case 'K':
x = 1;
y = 9;
break;
case 'L':
x = 2;
y = 0;
break;
case 'M':
x = 2;
y = 1;
break;
case 'N':
x = 2;
y = 2;
break;
case 'O':
x = 3;
y = 5;
break;
case 'P':
x = 2;
y = 3;
break;
case 'Q':
x = 2;
y = 4;
break;
case 'R':
x = 2;
y = 5;
break;
case 'S':
x = 2;
y = 6;
break;
case 'T':
x = 2;
y = 7;
break;
case 'U':
x = 2;
y = 8;
break;
case 'V':
x = 2;
y = 9;
break;
case 'W':
x = 3;
y = 2;
break;
case 'X':
x = 3;
y = 0;
break;
case 'Y':
x = 3;
y = 1;
break;
case 'Z':
x = 3;
y = 3;
break;
}
return null;
}
public static int[] charToInteger(char[] idChar){
int[] n = new int[idChar.length];
for(int i =1; i < idChar.length; i++){
n[i] = idChar[i] & '\u000f';
}
return n;
}
}
沒有留言 :
張貼留言