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);
    }
}


目录
相关文章
|
8月前
|
Java
SDUT lab 1.3
SDUT lab 1.3
52 0
|
8月前
|
计算机视觉
Lab
Lab
116 1
|
8月前
sdut链表lab2
sdut链表lab2
52 1
|
8月前
|
存储
sdut 链表lab1
sdut 链表lab1
39 1
|
8月前
|
Java
sdut lab1.4
sdut lab1.4
42 0
|
8月前
|
存储 Java 数据安全/隐私保护
sdut lava lab7.5
sdut lava lab7.5
53 0
|
8月前
SDUT JAVAlab3.8(法2)
SDUT JAVAlab3.8(法2)
34 0
|
8月前
SDUT JAVAlab3.2
SDUT JAVAlab3.2
34 0
|
8月前
SDUT JAVAlab3.5
SDUT JAVAlab3.5
33 0
|
8月前
|
Java
SDUT JAVAlab4.1
SDUT JAVAlab4.1
35 0