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.

1.png

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. 2.5 作业

    2024-04-15 08:48:02       46 阅读
  2. 假期2.5

    2024-04-15 08:48:02       49 阅读
  3. 2024/2/5

    2024-04-15 08:48:02       46 阅读
  4. 作业2024/2/5

    2024-04-15 08:48:02       45 阅读
  5. 2.5作业

    2024-04-15 08:48:02       44 阅读
  6. 作业2.5

    2024-04-15 08:48:02       47 阅读
  7. HTML<span style='color:red;'>5</span>(<span style='color:red;'>2</span>)

    HTML5(2)

    2024-04-15 08:48:02      26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-15 08:48:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-15 08:48:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-15 08:48:02       87 阅读
  4. Python语言-面向对象

    2024-04-15 08:48:02       96 阅读

热门阅读

  1. 手写一个民用Tomcat (04)

    2024-04-15 08:48:02       32 阅读
  2. 批量clone某个github用户的所有项目

    2024-04-15 08:48:02       41 阅读
  3. go并发编程以及socket通信的理解

    2024-04-15 08:48:02       35 阅读
  4. npm vs. pnpm vs. Yarn: 三者之间的区别与比较

    2024-04-15 08:48:02       38 阅读
  5. 数据可视化开发教程和案例

    2024-04-15 08:48:02       38 阅读