**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):

  1. 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.
  2. Now add all single-digit numbers from Step 1.
  3. Add all digits in the odd places from right to left in the card number.
  4. Sum the results from Step 2 and Step 3.
  5. 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);elseSystem.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;elsereturn 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;elsereturn (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.要学习相应的代码编辑器的一些常用快捷键,如:快速对齐等等

第六章第三十一题(金融应用:信用卡号的合法性验证)(Financial: credit card number validation)相关推荐

  1. 第七章第三十一题(合并两个有序列表)(Merge two ordered tables)

    #第七章第三十一题(合并两个有序列表)(Merge two ordered tables) **7.31(合并两个有序列表)编写下面的方法,将两个有序列表变成一个新的有序列表. public stat ...

  2. Java黑皮书课后题第6章:**6.31(金融应用:信用卡号的合法性验证)和**6.32 编写程序,提示用户输入一个long型整数的信用卡号码,显示这个数字是合法的还是非法的

    6.31(金融应用:信用卡号的合法性验证)编写程序,提示用户输入一个long型整数的信用卡号码,显示这个数字是合法的还是非法的 6.31题目 题目描述 破题 6.31代码 6.32 代码 运行实例 6 ...

  3. 金融应用:信用卡号的合法性验证

    信用卡号遵循某种模式.一个信用卡号必须是13到16位的整数.它的开头必须是: 4,指Visa卡 5,指Master卡 37,指American Express 卡 6,指Discover卡 1954年 ...

  4. 第五章第三十一题(金融应用:计算CD价值)(Financial application: compute CD value)

    *5.31(金融应用:计算CD价值)假设你用10000美元投资一张CD,年获利率为5.75%. 一个月后,这张CD价值为 10000 + 10000 * 5.75 / 1200 = 10047.92 ...

  5. 第六章第十五题(金融应用:打印税表)(Financial application: print a tax table)

    第六章第十五题(金融应用:打印税表)(Financial application: print a tax table) *6.15(金融应用:打印税表)程序清单3-5给出了计算税款的程序.使用税款的 ...

  6. 内储说下六微第三十一

      内储说下六微第三十一 所谓六微,指的是臣下和外敌危害君权的六种隐微手段,韩非在本文中以大量生动的历史.传说故事告诫君主要提高警惕加强防范,在国内杜绝六微,在国外运用六微,达到巩固君权的目的. 六微 ...

  7. 04737 c++ 自学考试2019版 第六章课后练习 程序设计题 1

    /* * 04737 c++ 自学考试2019版 第六章课后练习 * 程序设计题 1 * 需求:将第五章习题中设计的交通工具...... */#include<iostream> #inc ...

  8. 第六章贪心(三):排序不等式、绝对值不等式

    第六章贪心(三):排序不等式.绝对值不等式.推公式 AcWing 913:排队打水 题目 有 n 个人排队到 1 个水龙头处打水,第 i 个人装满水桶所需的时间是 ti,请问如何安排他们的打水顺序才能 ...

  9. 第三章第三十题(当前时间)(Current time)

    第三章第三十题(当前时间)(Current time) *3.30(当前时间)修改编程练习题2.8,以12小时时钟制显示小时数. 下面是一个运行示例: Enter the time zone offs ...

  10. Java语言程序设计数据结构基础篇第11版6.31(金融应用:信用卡号的合法性检验)信用卡号遵循某种模式。一个信用卡号必须是13-16位的整数 (java)

    信用卡号遵循某种模式.一个信用卡号必须是13到16位的整数.它的开头必须是: 4,指Visa卡 5,指Master卡 37,指American Express卡 6,指Discover卡 1954年, ...

最新文章

  1. 设计模式心得笔记--简单工厂
  2. Word论文写作如何实现公式居中、编号右对齐
  3. 藉上帝之旨,行时代之命的文学长征
  4. wpa_supplicant 无线网络配置
  5. 解决 https 证书验证不通过的问题
  6. 算法学习笔记:连通图详解
  7. vss2005 配置与使用
  8. python推荐书豆瓣_基于Python的豆瓣图书评论数据获取与可视化分析
  9. js与朴php订单评价功能
  10. 条件概率公式、全概率公式以及贝叶斯公式
  11. PAKDD2018小结
  12. Android登陆demo:界面设计及业务代码———Android菜鸟的成长日记
  13. InstantNGP
  14. 山寨“苹果皮”上市或涉嫌侵权iPhon
  15. Windows下mysql的下载和安装
  16. 汽车散热器不同造型的注塑件管口密封方案
  17. 某校2019专硕编程题-平方根
  18. 白名单认证 solidity 代码
  19. 需求工程——软件建模与分析阅读笔记04
  20. Oracle小知识点之temp表空间

热门文章

  1. 博客好助手——截图工具Snipaste试用
  2. Android基于Ymodem协议升级嵌入式MCU主控
  3. 眼科准分子激光治疗仪行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  4. 正确的座机号码格式_正确的填写手机号码的格式是什么?
  5. 用MUI花两天时间快速开发『One·一个』App,兼容Android、iOS双平台
  6. oracle中的中文排序,Oracle下的中文排序
  7. 解决代理服务器端口被占用
  8. win7nodejs压缩包配置环境变量
  9. Laravel 使用百度地图实现地理位置转经纬度
  10. 51单片机控制数码管显示hello,012345,以及apple自动切换