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

 Kathy-辜 发布于 2023-02-08 15:10

我尝试使用Luhn算法检查信用卡的验证,该算法的工作原理如下:

    从右到左加倍每秒.如果数字加倍会产生两位数字,请将两位数相加以得到一位数字.

    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

    现在添加步骤1中的所有单位数字.

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

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

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

    对步骤2和步骤3的结果求和.

    37 + 38 = 75

    如果步骤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;
    }
     }

Nicholas Ng.. 24

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

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

2 个回答
  • 谷歌和维基百科是你的朋友.我将使用int-array而不是long-array.在Wikipedia上发布以下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;
       }
    

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

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

    2023-02-08 15:13 回答
  • 您可以自由导入以下代码:

    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

    2023-02-08 15:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有