SDUT lab5-2

简介: SDUT lab5-2

7-2 sdut-JAVA-Credit Card Number Validation

分数 10

全屏浏览

切换布局

作者 马新娟

单位 山东理工大学

Each type of credit card begins with a prefix or range of prefixes and is of a certain length. Table 1

shows the details of two commonly used credit card types.

In addition to these rules a credit card number is subjected to the Luhn formula to determine if it is valid.

The following are the steps of this formula:

1. Take each odd positioned digit and multiply it by 2. If the result of multiplying one such digit by 2 is a 2-digit number these digits should be summed so that the result is a single digit.

2. Add results of all multiplications in Step 1.

3. Sum all even positioned digits.

4. Add the result of Step 2. to the result of Step 3.

5. Modulus the result of Step 4. with 10. If the result is 0 this indicates that the credit card number is

valid.

e.g., Credit Card Number 4567123474567836

Step 1 and Step 2: (4 * 2) + (6 * 2) + (1 * 2) + (3 * 2) + (7 * 2) + (5 * 2) + (7 * 2) + (3 * 2)

= 8 + 12 + 2 + 6 + 14 + 10 + 14 + 6

= 8 + 3 + 2 + 6 + 5 + 1 + 5 + 6

= 36

Step 3: 5 + 7 + 2 + 4 + 4 + 6 + 8 + 6

= 42

Step 4: 36 + 42

= 78

Step 5: 78 modulus 10 = 8 thus invalid

You have been requested to assist with the development of a solution to check if a credit card number is valid. You should create an array of integers,whose length is 16. You should assume (i) the values in this array have passed the criteria in Table 1 and (ii) that in each location of this array is an integer in the range 0 to 9. You are required to apply the steps 1 to 5 to the values in this array and state whether or not the values represent a valid credit card. You should modify your solution so that you place the values into the array in your main() method and then pass these values (a reference to these values) to another method titled luhnFormula(). The luhnFormula() method will return true to your main() method if the values constitute a valid credit card number otherwise it will return false. Your main() method should display a message stating whether or not the values constitute a valid credit card number.

Input Specification:

You should create an array of integers,then input all the numbers for it.

Output Specification:

Your main() method should display a message stating whether or not the values constitute a valid credit card number.

Sample Input:

4
5
6
7
1
2
3
4
7
4
5
6
7
8
3
6

Sample Output:

Invalid Credit Card Number

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in );
        int[] cardNumber = new int[16];
        for (int i = 0; i < 16; i++) {
            cardNumber[i] = scanner.nextInt();
        }
        if (isValidCreditCardNumber(cardNumber)) {
            System.out.println("Valid Credit Card Number");
        } else {
            System.out.println("Invalid Credit Card Number");
        }
    }
 
    public static boolean isValidCreditCardNumber(int[] cardNumber) {
        int sum = 0;
        boolean alternate = false;
        for (int i = cardNumber.length - 1; i >= 0; i--) {
            int n = cardNumber[i];
            if (alternate) {
                n *= 2;
                if (n > 9) {
                    n = (n % 10) + 1;
                }
            }
            sum += n;
            alternate = !alternate;
        }
        return (sum % 10 == 0);
    }
}


目录
相关文章
|
1月前
|
安全 数据安全/隐私保护
sqli-lab教程Less-4
sqli-lab教程Less-4
21 0
|
1月前
|
关系型数据库 MySQL Shell
sqli-lab教程Less-7
sqli-lab教程Less-7
37 0
|
1月前
|
安全
sqli-lab教程Less-9
sqli-lab教程Less-9
36 0
|
1月前
|
Java
SDUT lab 1.3
SDUT lab 1.3
28 0
|
1月前
|
计算机视觉
Lab
Lab
20 1
|
1月前
|
存储 Java 数据安全/隐私保护
sdut lava lab7.5
sdut lava lab7.5
23 0
|
1月前
|
Java
sdut lab1.4
sdut lab1.4
25 0
|
1月前
|
安全 数据安全/隐私保护
sqli-lab教程Less-6
sqli-lab教程Less-6
42 0
|
监控 流计算
dc_labs--lab1的学习与总结
本节为dc_labs系列的第一篇,主要根据自己对于lab的理解,简述实验的过程,同时对于笔者自己觉得需要进一步理解的进行总结学习。本节重点在于理解启动文件与DC的综合流程。建议与对应博文([DC学习笔记正式篇之零——综述与基本流程介绍](https://guodongblog.com/posts/80912d01b675/))进行结合起来进行学习。该文为对应部分的实践篇的内容。本系列博文不会只是带着进行实验内容,个人觉得单纯跑一遍实验意义不大。会结合自己学习的理解进行部分展开。有问题欢迎留言一起学习。
769 0
|
IDE 开发工具 Python
Easy Games With Python and Pygame(一)- Pygame Quickstart
Easy Games With Python and Pygame(一)- Pygame Quickstart
Easy Games With Python and Pygame(一)- Pygame Quickstart