Last active
August 29, 2015 14:01
-
-
Save HabaCo/05ed1b0982795714db3f to your computer and use it in GitHub Desktop.
身分證產生器 (精簡)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Random; | |
| /* ========== 身分證產生器 ========== */ | |
| public class IDMaker { | |
| static String checkHead = "ABCDEFGHJKLMNPQRSTUVWXYZIO"; | |
| public static void main(String[] args) { | |
| Random r = new Random(); | |
| String s = ""; | |
| int checknum = 0; // 產生前9碼的同時計算產生驗證碼 | |
| // 產生第一個英文字母 | |
| int t=(r.nextInt(26)+65); | |
| s+=(char)t; | |
| t=checkHead.indexOf((char)t)+10; | |
| checknum += t/10; | |
| checknum += t%10*9; | |
| // 產生第2個數字 (1~2) | |
| s += Integer.toString(t = r.nextInt(2)+1); | |
| checknum += t*8; | |
| // 產生後8碼 | |
| for (int i=2; i<9; i++){ | |
| s += Integer.toString(t = r.nextInt(10)); | |
| checknum += t*(9-i); | |
| } | |
| // 完成驗證碼計算 | |
| checknum = (10-((checknum)%10))%10; | |
| s+=Integer.toString(checknum); | |
| System.out.println(s); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment