6.31(財務應用程序:信用卡號的合法性)信用卡號遵循下面的模式。一個信用卡號必須是13到16位的整數。它的開頭必須是:java
在1954年,IBM的Hans Luhn提出一種算法,該算法能夠驗證信用卡號的有效性。這個算法在肯定輸入的卡號是否正確,或者這張信用卡號是否被掃描儀正確掃描方面是很是有用的。遵循這個合法性檢 測能夠生成全部的信用卡號,一般稱之爲Luhn檢測或者Mod 10檢測,能夠以下描述(爲了方便解釋,假設卡號爲4388576018402626):git
1)從右到左對每一個數字翻倍。若是對某個數字翻倍以後的結果是一個兩位數,那麼就將這兩位加在一塊兒獲得一位數。算法
2) 如今將第一步獲得的全部一位數相加。
4+4+8+2+3+1+7+8=37
3) 將卡號裏從右到左在奇數位上的全部數字相加。
6+6+0+8+0+7+8+3=38
4) 將第二步和第三步獲得的結果相加。
37+38=75
5) 若是第四步獲得的結果能被10整除,那麼卡號是合法的;不然,卡號是不合法的。例
如,號碼4388576018402626是不合法的,可是號碼4388576018410707是合法的。less
編寫程序,提示用戶輸入一個long型整數的信用卡號碼,顯示這個數字是合法的仍是非法
的。使用下面的方法設計程序:this
1 /** Returntrue if the card number is valid */ 2 public static boolean isVa1id(long number) 3 4 /** Getthe result from Step2 */ 5 public static int sumOfDoubleEvenPIace(long number) 6 /** Return this number if it is a single digit, otherwise, 7 * return the sum of the two digits*/ 8 public static int getDigit(int number) 9 /** Returnsum ofodd-placedigitsin number */ 10 public static int sumOfOddPlace(1ong number) 11 /** Returntrue if the digitd is a prefix for number */ 12 public static boolean prefixMatched(1ong number, int d) 13 /** Returnthe number ofdigitsin d */ 14 public static int getSize(1ong d) 15 /** Return the first k number ofdigitsfrom number. If the 16 * number ofdigitsin number is less than k, return number. */ 17 public static long getPrefix(long number, int k)
下面是程序的運行示例:(你也能夠經過將輸人做爲一個宇符串讀人,以及對宇符串進行處
理來驗證信用卡卡號。spa
如下爲實現這功能的代碼:設計
1 /** fileName: creditCardVerify.java 2 * 做用: 計算信用卡號的合法性 3 * mail: xuangliang1@live.com 4 * 說明: 信用卡號必須是13到16位,開頭必須是4,5,37,6 5 * 6 */ 7 8 import java.util.Scanner; 9 10 public class creditCardVerify{ 11 public static void main(String[] args) { 12 Scanner input = new Scanner(System.in); 13 System.out.print("請輸入卡號,按Enter結束輸入: "); 14 long i = input.nextLong(); 15 if (isValid(i)) 16 System.out.println(i + " is valid"); 17 else 18 System.out.println(i + " is invalid"); 19 } 20 21 /** Return true if the card number is valid */ 22 /** 返回ture代表這個卡號是有效的 */ 23 public static boolean isValid(long number) { 24 if(prefixMatched(number)){ 25 if(sumOfdoubleEvenPlace(number)%10 == 0) 26 return true; 27 } 28 return false; 29 } 30 31 /** 32 * Get the result from Step 2 從步驟2獲得結果?。雙重偶數之和 33 */ 34 public static int sumOfdoubleEvenPlace(long number) { 35 int sum = 0; 36 int sumGetdigit = 0; 37 long temp = 0; 38 int numberSize = getSize(number); 39 for(int i =2; i <= numberSize; i += 2){ 40 temp = getPrefix(number, i); 41 sumGetdigit += getDigit((int)temp*2); 42 } 43 sum = sumGetdigit + sumOfOddPlace(number); 44 return sum; 45 } 46 47 /** 48 * Retrun this number if it is a single digit, otherwise, Return the sum of the 49 * two digits 50 * 51 * 若是是單個數字,則返回該數字,不然返回兩位數的和。得到數字 52 */ 53 public static int getDigit(int number) { 54 int numGetDigit = 0; 55 if(number % 10 >= 0){ 56 numGetDigit = number % 10; 57 number /= 10; 58 numGetDigit += number; 59 return numGetDigit; 60 } 61 return number; 62 } 63 64 /** 65 * Return sum of odd-place digits in number 返回卡號總右往左的奇位數之和 66 */ 67 public static int sumOfOddPlace(long number) { 68 int sum = 0, i = 0; 69 int Size = getSize(number); 70 71 for(i = 1; i <= Size; i+=2){ 72 sum += getPrefix(number, i); 73 } 74 return sum; 75 } 76 77 /** 78 * Return true if the digit d is a prefix for number 判斷卡號的前綴是否合法 79 */ 80 public static boolean prefixMatched(long number) { 81 int numberSize = getSize(number); 82 if(numberSize >= 13 && numberSize <= 16){ 83 switch((int)getPrefix(number, numberSize)){ 84 case 4: return true; 85 case 5: return true; 86 case 6: return true; 87 case 3: if((int)getPrefix(number, numberSize -1) == 7) 88 return true; 89 } 90 } 91 return false; 92 } 93 94 /** Return the number of digits in d 95 * 得到信用卡號的長度並將結果返回 96 */ 97 public static int getSize(long d){ 98 long i= 0; 99 while(d > 0){ 100 i += 1; 101 d /= 10; 102 } 103 return (int)i; 104 } 105 106 /** Return the first k number of digits from number. If the 107 * Number of digits in number is less than k, return number. 108 * 從數字中返回第一個k位數。若是數字總的位數小於k,則返回數字。 109 * 應該是得到信用卡號的第k位的字母 110 */ 111 public static long getPrefix(long number, int k){ 112 int i = 0; 113 long temp=0; 114 while (i < k){ 115 i++; 116 temp = number % 10; 117 number /= 10; 118 } 119 return temp; 120 } 121 }