使用 Luhn 算法检查信用卡有效性 [英] Check Credit Card Validity using Luhn Algorithm

查看:36
本文介绍了使用 Luhn 算法检查信用卡有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Luhn 算法检查信用卡的验证,其工作步骤如下:

I tried to check the validation of credit card using Luhn algorithm, which works as the following steps:

  1. 从右到左每第二个数字翻一番.如果一位数加倍得到两位数,则将两位数相加得到一位数.

  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 * 2 = 4

2 * 2 = 4

4 * 2 = 8

1 * 2 = 2

6 * 2 = 12 (1 + 2 = 3)

6 * 2 = 12 (1 + 2 = 3)

5 * 2 = 10 (1 + 0 = 1)

5 * 2 = 10 (1 + 0 = 1)

8 * 2 = 16 (1 + 6 = 7)

8 * 2 = 16 (1 + 6 = 7)

4 * 2 = 8

现在添加第 1 步中的所有个位数.

Now add all single-digit numbers from Step 1.

4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

添加卡号从右到左奇数位的所有数字.

Add all digits in the odd places from right to left in the card number.

6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

将第 2 步和第 3 步的结果相加.

Sum the results from Step 2 and Step 3.

37 + 38 = 75

37 + 38 = 75

如果第4步的结果能被10整除,则卡号有效;否则无效.例如号码4388576018402626无效,但号码4388576018410707有效.

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.

简单地说,我的程序始终显示对我输入的所有内容有效.即使它是一个有效数字并且 sumOfOddPlace 和 sumOfDoubleEvenPlace 方法的结果等于零.
感谢任何帮助.

Simply, my program always displays valid for everything that I input. Even if it's a valid number and the result of sumOfOddPlace and sumOfDoubleEvenPlace methods are equal to zero.
Any help is appreciated.

import java.util.Scanner;
public class CreditCardValidation {
      public static void main(String[] args) {
     Scanner in = new Scanner(System.in);
        int count = 0;
        long array[] = new long [16];
       do
       {
        count = 0;
       array = new long [16];
        System.out.print("Enter your Credit Card Number : ");
        long number = in.nextLong();
        for (int i = 0; number != 0; i++) {
        array[i] = number % 10;
        number = number / 10;
        count++;
        }
       }
        while(count < 13); 
        if ((array[count - 1] == 4) || (array[count - 1] == 5) || (array[count - 1] == 3 && array[count - 2] == 7)){
            if (isValid(array) == true) {
                System.out.println("\n The Credit Card Number is Valid. ");
        } else {
            System.out.println("\n The Credit Card Number is Invalid. ");
        }
        } else{
          System.out.println("\n The Credit Card Number is Invalid. ");
        }
    }

    public static boolean isValid(long[] array) {
        int total = sumOfDoubleEvenPlace(array) + sumOfOddPlace(array);        
        if ((total % 10 == 0)) {
         for (int i=0; i< array.length; i++){
            System.out.println(array[i]);}
            return true;
        } else {
          for (int i=0; i< array.length; i++){
            System.out.println(array[i]);}
            return false;
        }
    }

    public static int getDigit(int number) {
        if (number <= 9) {
            return number;
        } else {
            int firstDigit = number % 10;
            int secondDigit = (int) (number / 10);
            return firstDigit + secondDigit;
        }
    }

    public static int sumOfOddPlace(long[] array) {
        int result = 0;
        for (int i=0; i< array.length; i++)
        {
        while (array[i] > 0) {
            result += (int) (array[i] % 10);
            array[i] = array[i] / 100;
         }}
         System.out.println("\n The sum of odd place is " + result);
        return result;
    }

    public static int sumOfDoubleEvenPlace(long[] array) {
        int result = 0;
        long temp = 0;
        for (int i=0; i< array.length; i++){
        while (array[i] > 0) {
             temp = array[i] % 100;
             result += getDigit((int) (temp / 10) * 2);
            array[i] = array[i] / 100;
           }
        }
        System.out.println("\n The sum of double even place is " + result);
        return result;
    }
     }

推荐答案

可以自由导入以下代码:

You can freely import the following code:

public class Luhn
{
    public static boolean Check(String ccNumber)
    {
            int sum = 0;
            boolean alternate = false;
            for (int i = ccNumber.length() - 1; i >= 0; i--)
            {
                    int n = Integer.parseInt(ccNumber.substring(i, i + 1));
                    if (alternate)
                    {
                            n *= 2;
                            if (n > 9)
                            {
                                    n = (n % 10) + 1;
                            }
                    }
                    sum += n;
                    alternate = !alternate;
            }
            return (sum % 10 == 0);
    }
}

链接参考:https://github.com/jduke32/gnuc-credit-card-checker/blob/master/CCCheckerPro/src/com/gnuc/java/ccc/Luhn.java

这篇关于使用 Luhn 算法检查信用卡有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆