Java黑皮书课后题第6章:**6.31(金融应用:信用卡号的合法性验证)和**6.32 编写程序,提示用户输入一个long型整数的信用卡号码,显示这个数字是合法的还是非法的
6.31(金融應用:信用卡號的合法性驗證)編寫程序,提示用戶輸入一個long型整數的信用卡號碼,顯示這個數字是合法的還是非法的
- 6.31題目
- 題目描述
- 破題
- 6.31代碼
- 6.32
- 代碼
- 運行實例
6.31題目
題目描述
6.31(金融應用:信用卡號的合法性驗證)信用卡號遵循某種模式。一個信用卡號必須是13到16位的整數,它的開頭必須是:
- 4,指Visa卡
- 5,指Master卡
- 37,指American Express卡
- 6,指Discover卡
1954年,IBM的Hans Luhn提出一種算法,用于驗證信用卡號的有效性。這個算法在確定輸入的卡號是否正確,或者這張信用卡是否被掃描儀正確掃描方面是非常有用的。遵循這個方法……(假設卡號為4388576018402626):
如:號碼4388576018402626非法,4388576018410707合法
編寫程序,提示用戶輸入一個long型整數的信用卡號碼,顯示這個數字是合法的還是非法的
使用下面的方法設計程序:
/** Return true if the card number is valid */
public static boolean isValid(long number)
// Get the result from Step 2:第二步
public static int sumOfDoubleEvenPlace(long number)
// Return this number if it is a single digit, otherwise,
// return the sum of the two digits:第一步
public static int getDigit(int number)
// Return sum of odd-place digits in number:第三步
public static int sumOfOddPlace(long number)
// Return true if the number d is a prefix for number:判斷前綴
public static boolean prefixMatched(long number, int d)
// Return the number of digits in d:獲取長度
public static int getSize(long d)
// Return the first k number of digits from number, If the 返回第一個k位數
// number of digits in number is less than k, return number:
public static long getPrefix(long number, int k)
可以改用String獲取輸入
下面是程序的運行示例:
破題
6.31代碼
import java.util.Scanner;public class Test6_31 {public static void main(String[] args) {// 獲取輸入Scanner input = new Scanner(System.in);System.out.print("Enter a credit card number as a long integer\n\t");String str = input.next();// 判斷str長度是否達到標準int length = getSize(str);if (length < 13 || length > 16){System.out.print(str + " is invalid");System.exit(1);}// 判斷str是否是由4、5、37、6為前綴boolean bool1 = prefixMatched(str, 4);boolean bool2 = prefixMatched(str, 5);boolean bool3 = prefixMatched(str, 37);boolean bool4 = prefixMatched(str, 6);if (bool1 || bool2 || bool3 || bool4){}else{System.out.print(str + " is invalid");System.exit(1);}// 將輸入內容傳遞給isValid方法并接收結果boolean result = isValid(str);// 判斷并輸出結果if (result)System.out.print(str + " is valid");elseSystem.out.print(str + " is invalid");}/** Return true if the card number is valid */public static boolean isValid(String number){int result = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);if (result % 10 == 0)return true;elsereturn false;}// 第二步:偶數位二倍相加(如果是兩位數則這兩位數相加public static int sumOfDoubleEvenPlace(String number){int length = getSize(number);int sum = 0;String temp = "";for (int i = 1; i < length;i+=2){temp = number.substring(i, i+1);sum += Math.pow(Integer.parseInt(temp), 2);}return sum;}// 第三步:奇數位相加public static int sumOfOddPlace(String number){int length = getSize(number);int sum = 0;String temp = "";for (int i = 1; i < length;i+=2){temp = number.substring(i, i+1);sum += Math.pow(Integer.parseInt(temp), 2);}return sum;}// 判斷長度public static int getSize(String d){return d.length();}// 判斷前綴public static boolean prefixMatched(String number, int d){if (d < 10){String temp = number.substring(0,1);int start = Integer.parseInt(temp);if (start >= 4 && start <= 6)return true;elsereturn false;}else{String temp = number.substring(0,2);int str_int = Integer.parseInt(temp);if (str_int == 37)return true;elsereturn false;}} }6.32
代碼
因為6.30和6.32兩道題題目內容有些問題
可能時不時會被下線
博主還在不斷修改、完善內容
運行實例
2161Process finished with exit code 0總結
以上是生活随笔為你收集整理的Java黑皮书课后题第6章:**6.31(金融应用:信用卡号的合法性验证)和**6.32 编写程序,提示用户输入一个long型整数的信用卡号码,显示这个数字是合法的还是非法的的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第6章:**6.29
- 下一篇: Java黑皮书课后题第6章:**6.34