**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
下面是参考答案代码:
import java.util.*;
public class CreditCardNumberValidationQuestion31 {
public static void main(String[] args) {
long creditCardNumber;
Scanner inputScanner = new Scanner(System.in);
System.out.print("Enter a credit card number as a long integer: ");
creditCardNumber = inputScanner.nextLong();
if(isValid(creditCardNumber))
System.out.printf("%d is valid", creditCardNumber);
else
System.out.printf("%d is invalid", creditCardNumber);
inputScanner.close();
}
/** Return true if the card number is valid */
public static boolean isValid(long number){
if((13 <= getSize(number) && getSize(number) <= 16)
&& (prefixMatched(number, 4) || prefixMatched(number, 5)
|| prefixMatched(number, 37) || prefixMatched(number, 6))
&& ((sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0))
return true;
else
return false;
}
/** Get the result from Step 2 */
public static int sumOfDoubleEvenPlace(long number){
String numberString = String.valueOf(number);
int sum = 0;
for(int i = getSize(number) - 2;i >= 0 ;i -= 2)
sum += getDigit(2 * Integer.parseInt(String.valueOf(numberString.charAt(i))));
return sum;
}
/** Return this number if it is a single digit, otherwise,
* return the sum of the two digits */
public static int getDigit(int number){
if(number <= 9)
return number;
else
return (number / 10 + number % 10);
}
/** Return sum of odd-place digits in number */
public static int sumOfOddPlace(long number) {
String numberString = String.valueOf(number);
int sum = 0;
for(int i = getSize(number) - 1;i >= 0 ;i -= 2)
sum += Integer.parseInt(String.valueOf(numberString.charAt(i)));
return sum;
}
/** Return true if the number d is a prefix for number */
public static boolean prefixMatched(long number, int d) {
//System.out.println(getPrefix(number,getSize(d)));
return d == getPrefix(number,getSize(d));
}
/** Return the number of digits in d */
public static int getSize(long d) {
String dString = String.valueOf(d);
return dString.length();
}
/** Return the first k number of digits from number. If the
* number of digits in number is less than k, return number. */
public static long getPrefix(long number, int k) {
if(getSize(number) < k){
return number;
}
long prefixNumber = number / (long)Math.pow(10, getSize(number) - k);
return prefixNumber;
}
}
运行效果:
注:编写程序要养成良好习惯
1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)
5.普通变量,方法名要小驼峰,类名要大驼峰,常量要使用全部大写加上下划线命名法
6.要学习相应的代码编辑器的一些常用快捷键,如:快速对齐等等