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


目录
相关文章
|
机器学习/深度学习 算法 机器人
Tensorflow车牌识别完整项目(含完整源代码及训练集)
Tensorflow车牌识别完整项目(含完整源代码及训练集)
1271 0
Tensorflow车牌识别完整项目(含完整源代码及训练集)
|
Java
SDUT Java 2.3
SDUT Java 2.3
112 0
|
11月前
|
Java 数据库连接 数据库
【潜意识Java】深度分析黑马项目《苍穹外卖》在Java学习中的重要性
《苍穹外卖》项目对Java学习至关重要。它涵盖了用户管理、商品查询、订单处理等模块,涉及Spring Boot、MyBatis、Redis等技术栈。
1326 4
|
Java
sdut java lab 7.1(法二好理解)
sdut java lab 7.1(法二好理解)
112 1
|
Java
SDUT JAVA lab3.1
SDUT JAVA lab3.1
128 0
|
存储 SQL 算法
动态规划Dynamic programming详解-硬币找零问题【python】
动态规划Dynamic programming详解-硬币找零问题【python】
7-9 sdut-C语言实验-免费馅饼
7-9 sdut-C语言实验-免费馅饼
92 1
|
存储 人工智能 算法
7-1 sdut-C语言实验-活动选择
7-1 sdut-C语言实验-活动选择
173 1
sdut链表lab2
sdut链表lab2
125 1
链表4(法二)------7-4 sdut-C语言实验-单链表中重复元素的删除
链表4(法二)------7-4 sdut-C语言实验-单链表中重复元素的删除
191 0