**6.31(金融應用:信用卡號的合法性驗證)信用卡號遵循某種模式,一個信用卡號必須是13到16位的整數,它的開頭必須是:
4,指Visa卡
5,指Master卡
37,指American Express 卡
6,指Discover卡
1954年,IBM的Hans Luhn提出一種演算法,用于驗證信用卡號的有效性,這個演算法在確定輸入的卡號是否正確,或者這張信用卡是否被掃描儀正確掃描方面是非常有用的,遵循這個合法性檢測可以生成所有的信用卡號,通常稱之為Luhn檢測或者Mod 10檢測,可以如下描述(為了方便解釋,假設卡號4388576018402626):
1.從右到左對偶數位數字翻倍,如果對某個數字翻倍之后的結果是一個兩位數,那么就將這兩位加在一起得到一位數,
2.現在將第一步得到的所有一位數相加,
3.將卡號里從右到左奇數位上的所有數字相加,
4.將第二步和第三步得到的結果相加,
5.如果第四步得到的結果能被10整除,那么卡號是合法的;否則,卡號是不合法的,例如,號碼4388576018402626是不合法的,但是號碼4388576018410707是合法的,
撰寫程式,提示用戶輸入一個long型整數的信用卡號碼,顯示這個數字是合法的還是非法的,
使用下面的方法設計程式:
public static boolean isValid(long number)
public static int sumOfDoubleEvenPlace(long number)
public static int getDigit(int number)
public static int sumOfOddPlace(long number)
public static boolean prefixMatched(long number, int d)
public static int getSize(long d)
public static long getPrefix(long number, int k)
下面是程式的運行示例:(你也可以通過將輸入作為一個字串讀入,以及對字串進行處理來驗證信用卡卡號,)
Enter a credit card number as a long integer: 4388576018410707
4388576018410707 is valid
Enter a credit card number as a long integer: 4388576018402626
4388576018402626 is invalid
**6.31(Financial: credit card number validation) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with
4 for Visa cards
5 for Master cards
37 for American Express cards
6 for Discover cards
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly, or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626):
Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
Now add all single-digit numbers from Step 1.
Add all digits in the odd places from right to left in the card number.
Sum the results from Step 2 and Step 3.
If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.
Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid.
Design your program to use the following methods:
public static boolean isValid(long number)
public static int sumOfDoubleEvenPlace(long number)
public static int getDigit(int number)
public static int sumOfOddPlace(long number)
public static boolean prefixMatched(long number, int d)
public static int getSize(long d)
public static long getPrefix(long number, int k)
Here are sample runs of the program: (You may also implement this program by reading the input as a string and processing the string to validate the credit card.)
Enter a credit card number as a long integer: 4388576018410707
4388576018410707 is valid
Enter a credit card number as a long integer: 4388576018402626
4388576018402626 is invalid
下面是參考答案代碼:
// https://cn.fankuiba.com
import java.util.Scanner;
public class Ans6_31_page203 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a credit card number as a long integer:");
long number = input.nextLong();
if (isVa1id(number) && divide(number))
System.out.println(number + " is valid");
else
System.out.println(number + " is invalid");
}
// Step 0
public static boolean isVa1id(long number) {
if (prefixMatched(number,4) || prefixMatched(number,5) ||
prefixMatched(number,37) || prefixMatched(number,6)) {
return (number + "").length() >= 13 && (number + "").length() <= 16;
}
else
return false;
}
// Get the result from Step 2
public static int getEven(long number) {
int getEven = 0;
for (int i = 0; i < (number+"").length(); i+=2) {
char n = (number + "").charAt(i);
int num = Integer.parseInt(Character.toString(n));
if (num * 2 > 9)
num = (num * 2 / 10) + (num * 2 % 10);
else if (num * 2 < 10)
num = num * 2;
getEven += num;
}
return getEven;
}
// Get the result from Step 3
public static int getOdd(long number) {
int getOdd = 0;
for (int i = 1; i < (number+"").length(); i+=2) {
char n = (number + "").charAt(i);
int num = Integer.parseInt(Character.toString(n));
getOdd += num;
}
return getOdd;
}
// Return sum
public static int sumOfOddAndEven(long number) {
return getEven(number) + getOdd(number);
}
// Return true if the digit d is a prefix for number
public static boolean prefixMatched(long number, int d) {
String strNumber = number + "";
int charNumber0 = Integer.parseInt(strNumber.substring(0,(d+"").length()));
return charNumber0 == d;
}
public static boolean divide(long d) {
return sumOfOddAndEven(d) % 10 == 0;
}
}
適用Java語言程式設計與資料結構(基礎篇)(原書第11版)Java語言程式設計(基礎篇)(原書第10/11版)更多內容
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/187331.html
標籤:Java
上一篇:JSR303后端校驗詳細筆記
下一篇:python(遞回函式)
