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

新手上路,请多包涵

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

  1. 从右到左每第二个数字加倍。如果将一个数字加倍得到一个两位数,将这两个数字相加得到一个单位数。

    2 * 2 = 4

    2 * 2 = 4

    4 * 2 = 8

    1 * 2 = 2

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

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

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

    4 * 2 = 8

  2. 现在将步骤 1 中的所有个位数相加。

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

  3. 将卡号中奇数位的所有数字从右到左相加。

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

  4. 将步骤 2 和步骤 3 的结果相加。

    37 + 38 = 75

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

简单地说,我的程序总是对我输入的所有内容显示有效。即使它是一个有效数字并且 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;
    }
     }

原文由 user3126388 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 541
1 个回答

您可以自由导入以下代码:

 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

原文由 Nicholas Ng 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏