使用Luhn算法检查信用卡有效性。

时间:2021-04-26 17:03:33

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

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

  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

    2 * 2 = 4

    2 * 2 = 4

    4 * 2 = 8

    4 * 2 = 8

    1 * 2 = 2

    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

    4 * 2 = 8

  2. Now add all single-digit numbers from Step 1.

    现在添加步骤1中的所有个位数。

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

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

  3. 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

  4. Sum the results from Step 2 and Step 3.

    总结步骤2和步骤3的结果。

    37 + 38 = 75

    37 + 38 = 75

  5. 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.

    如果第4步的结果可以被10整除,那么卡号是有效的;否则,它是无效的。例如,数字4388576018402626是无效的,但是数字4388576018410707是有效的。

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.

简单地说,我的程序总是对我输入的所有内容显示有效。即使它是一个有效的数,sumOfOddPlace和sumOfDoubleEvenPlace方法的结果都等于零。任何帮助都是感激。

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

5 个解决方案

#1


24  

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

Link reference: https://github.com/jduke32/gnuc-credit-card-checker/blob/master/CCCheckerPro/src/com/gnuc/java/ccc/Luhn.java

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

#2


9  

Google and Wikipedia are your friends. Instead of long-array I would use int-array. On Wikipedia following java code is published (together with detailed explanation of Luhn algorithm):

谷歌和*是你的朋友。我将使用int-array代替长数组。在*上公布了以下java代码(并详细解释了Luhn算法):

   public static boolean check(int[] digits) {
     int sum = 0;
     int length = digits.length;
     for (int i = 0; i < length; i++) {

       // get digits in reverse order
       int digit = digits[length - i - 1];

       // every 2nd number multiply with 2
       if (i % 2 == 1) {
           digit *= 2;
       }
       sum += digit > 9 ? digit - 9 : digit;
     }
     return sum % 10 == 0;
   }

You should work on your input processing code. I suggest you to study following solution:

您应该处理输入处理代码。我建议你研究以下解决方案:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    boolean repeat;
    List<Integer> digits = new ArrayList<Integer>();

    do {
        repeat = false;
        System.out.print("Enter your Credit Card Number : ");
        String input = in.next();

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c < '0' || c > '9') {
                repeat = true;
                digits.clear();
                break;
            } else {
                digits.add(Integer.valueOf(c - '0'));
            }
        }
    } while (repeat);

    int[] array = new int[digits.size()];
    for (int i = 0; i < array.length; i++) {
        array[i] = Integer.valueOf(digits.get(i));
    }
    boolean valid = check(array);
    System.out.println("Valid: " + valid);
}

#3


1  

I'll use 5 digit card numbers for simplicity. Let's say your card number is 12345; if I read the code correctly, you store in array the individual digits:

为了简单起见,我将使用5位数的卡号。假设你的卡号是12345;如果我正确地阅读了代码,您将单个数字存储在数组中:

array[] = {1, 2, 3, 4, 5}

Since you already have the digits, in sumOfOddPlace you should do something like

既然你已经有了数字,在sumOfOddPlace你应该做一些类似的事情。

public static int sumOfOddPlace(long[] array) {
    int result = 0;
    for (int i = 1; i < array.length; i += 2) {
        result += array[i];
    }
    return result;
}

And in sumOfDoubleEvenPlace:

而在sumOfDoubleEvenPlace:

public static int sumOfDoubleEvenPlace(long[] array) {
    int result = 0;
    for (int i = 0; i < array.length; i += 2) {
        result += getDigit(2 * array[i]);
    }
    return result;
}

#4


0  

private static int luhnAlgorithm(String number){
    int n=0;
    for(int i = 0; i<number.length(); i++){
        int x = Integer.parseInt(""+number.charAt(i));
        n += (x*Math.pow(2, i%2))%10;
        if (x>=5 && i%2==1) n++;
    }
    return n%10;
}

#5


0  

I took a stab at this with Java 8:

我尝试使用Java 8:

public static boolean luhn(String cc) {
    final boolean[] dbl = {false};
    return cc
            .chars()
            .map(c -> Character.digit((char) c, 10))
            .map(i -> ((dbl[0] = !dbl[0])) ? (((i*2)>9) ? (i*2)-9 : i*2) : i)
            .sum() % 10 == 0;
}

Add the line

添加一行

            .replaceAll("\\s+", "")

Before

之前

            .chars()

If you want to handle whitespace.

如果您想处理空格。

Seems to produce identical results to

似乎产生了相同的结果

return LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(cc);

From Apache's commons-validator.

从Apache的commons-validator。

#1


24  

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

Link reference: https://github.com/jduke32/gnuc-credit-card-checker/blob/master/CCCheckerPro/src/com/gnuc/java/ccc/Luhn.java

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

#2


9  

Google and Wikipedia are your friends. Instead of long-array I would use int-array. On Wikipedia following java code is published (together with detailed explanation of Luhn algorithm):

谷歌和*是你的朋友。我将使用int-array代替长数组。在*上公布了以下java代码(并详细解释了Luhn算法):

   public static boolean check(int[] digits) {
     int sum = 0;
     int length = digits.length;
     for (int i = 0; i < length; i++) {

       // get digits in reverse order
       int digit = digits[length - i - 1];

       // every 2nd number multiply with 2
       if (i % 2 == 1) {
           digit *= 2;
       }
       sum += digit > 9 ? digit - 9 : digit;
     }
     return sum % 10 == 0;
   }

You should work on your input processing code. I suggest you to study following solution:

您应该处理输入处理代码。我建议你研究以下解决方案:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    boolean repeat;
    List<Integer> digits = new ArrayList<Integer>();

    do {
        repeat = false;
        System.out.print("Enter your Credit Card Number : ");
        String input = in.next();

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c < '0' || c > '9') {
                repeat = true;
                digits.clear();
                break;
            } else {
                digits.add(Integer.valueOf(c - '0'));
            }
        }
    } while (repeat);

    int[] array = new int[digits.size()];
    for (int i = 0; i < array.length; i++) {
        array[i] = Integer.valueOf(digits.get(i));
    }
    boolean valid = check(array);
    System.out.println("Valid: " + valid);
}

#3


1  

I'll use 5 digit card numbers for simplicity. Let's say your card number is 12345; if I read the code correctly, you store in array the individual digits:

为了简单起见,我将使用5位数的卡号。假设你的卡号是12345;如果我正确地阅读了代码,您将单个数字存储在数组中:

array[] = {1, 2, 3, 4, 5}

Since you already have the digits, in sumOfOddPlace you should do something like

既然你已经有了数字,在sumOfOddPlace你应该做一些类似的事情。

public static int sumOfOddPlace(long[] array) {
    int result = 0;
    for (int i = 1; i < array.length; i += 2) {
        result += array[i];
    }
    return result;
}

And in sumOfDoubleEvenPlace:

而在sumOfDoubleEvenPlace:

public static int sumOfDoubleEvenPlace(long[] array) {
    int result = 0;
    for (int i = 0; i < array.length; i += 2) {
        result += getDigit(2 * array[i]);
    }
    return result;
}

#4


0  

private static int luhnAlgorithm(String number){
    int n=0;
    for(int i = 0; i<number.length(); i++){
        int x = Integer.parseInt(""+number.charAt(i));
        n += (x*Math.pow(2, i%2))%10;
        if (x>=5 && i%2==1) n++;
    }
    return n%10;
}

#5


0  

I took a stab at this with Java 8:

我尝试使用Java 8:

public static boolean luhn(String cc) {
    final boolean[] dbl = {false};
    return cc
            .chars()
            .map(c -> Character.digit((char) c, 10))
            .map(i -> ((dbl[0] = !dbl[0])) ? (((i*2)>9) ? (i*2)-9 : i*2) : i)
            .sum() % 10 == 0;
}

Add the line

添加一行

            .replaceAll("\\s+", "")

Before

之前

            .chars()

If you want to handle whitespace.

如果您想处理空格。

Seems to produce identical results to

似乎产生了相同的结果

return LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(cc);

From Apache's commons-validator.

从Apache的commons-validator。