我正在尝试在Java中编写Luhn算法。
我目前的代码是:
import java.util.Scanner;
public class luhnAlgorithm {
public static void main(String[] args) {
System.out.println("main() : Entry Point");
Scanner input = new Scanner(System.in);
long num;
int digit;
int sum = 0;
System.out.println("Enter the digits of a credit card number : ");
num = input.nextLong();
while (num > 0) {
digit = (int) num % 10;
num = num / 10;
if (num % 2 != 0 ) {
digit *= 2;
}
if (digit > 9) {
digit -= 9;
}
sum += digit;
}
if(sum % 10 == 0) {
System.out.println("Credit card number is valid.");
}
else
System.out.println("Credit card number is invalid. Please try again.");
System.out.println("main() : Exit Point");
}
}
我遇到的问题是,当我输入有效的信用卡号码时,例如:4012888888881881(通过PayPal Test Credit Card Accounts),它说它无效。但是,当我输入我自己的借记卡号时,它表示它是有效的。
我知道这里有一些混乱的代码,我只是无法弄清楚是什么。
任何帮助将不胜感激,并提前感谢!
答案 0 :(得分:4)
我认为我知道问题出在哪里。你以错误的方式进行乘法运算:
if (num % 2 != 0 ) {
digit *= 2;
}
当num为奇数时,您将乘以数字,而当Luhn的算法中,当该数字位于从右端到左侧的数字的偶数位置时,您应该乘以数字。尝试考虑添加一些索引,这将有助于您了解某个数字是否在事件所在位置。
您可以考虑将整数拆分为数组,然后检查数组的索引是否为偶数,或者将某个索引添加到while循环中。
仔细研究这个https://en.wikipedia.org/wiki/Luhn_algorithm
例如,如果你有68,那么你有:第一次迭代:digit = 8,num = 6和sum = 8秒迭代:digit = 6,num = 0这里你应该将你的数字乘以2,因为它是打开的数字中的偶数位置,但你不这样做,sum = 14而不是20
答案 1 :(得分:0)
好的,我实际上已经明白了:)。
import java.util.Scanner;
/*
* Author : Jonathan Patterson
* Date : 10/28/15
* Program : Luhn Algorithm; validates credit card numbers
*/
public class luhnAlgorithm {
public static void main(String[] args) {
System.out.println("main() : Entry Point");
Scanner input = new Scanner(System.in);
long num;
double digit = 0;
int sum = 0;
int n = 1;
int i = 0;
System.out.println("Enter the digits of a credit card number : ");
num = input.nextLong();
while (num > 0) {
digit = num % 10;
num = num / 10;
System.out.println(n + " digit is : " + digit);
if (i % 2 != 0 ) {
digit *= 2;
}
System.out.println(n + " digit is : " + digit);
if (digit > 9) {
digit = (digit % 10) + 1;
}
else
digit *= 1;
System.out.println(n + " digit is : " + digit);
sum += digit;
n++;
i++;
}
System.out.println("Sum of the digits is : " +sum);
if(sum % 10 == 0) {
System.out.println("Credit card number is valid.");
}
else
System.out.println("Credit card number is invalid. Please try again.");
System.out.println("main() : Exit Point");
}
}
答案 2 :(得分:0)
为此添加其他答案,以防其他人找到该帖子。
有一个带有有效实现的bitbucket项目: https://bitbucket.org/javapda/npi-validator/src/master/
如果用于验证NPI提供者编号(这是Luhn算法的一种实现方式)。
package org.bitbucket.javapda.npi;
import java.util.ArrayList;
import java.util.List;
public class NpiValidator {
private String npi;
public NpiValidator(String npi) {
this.npi = npi.trim();
}
public boolean isValid() {
return npi.length() == 10 && complies();
}
private boolean complies() {
if (!npi.matches("[0-9]{10}")) {
return false;
}
Character lastDigit = npi.charAt(9);
List<Integer> everyOther = listWithEveryOtherDoubled(npi.substring(0, 9));
int sum = 0;
for (Integer num : everyOther) {
sum += sumOfDigits(num);
}
int total = sum + 24; // 24 to account for 80840
int units = total % 10;
int checkDigit = (units != 0) ? (10 - units) : units;
return (Character.getNumericValue(lastDigit) == checkDigit);
}
private List<Integer> listWithEveryOtherDoubled(String str) {
List<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < str.length(); i++) {
if (i % 2 == 0) {
nums.add(2 * Character.getNumericValue(str.charAt(i)));
} else {
nums.add(Character.getNumericValue(str.charAt(i)));
}
}
return nums;
}
private static int sumOfDigits(int number) {
int num = number;
int sum = 0;
while (num > 0) {
sum += (num % 10);
num = num / 10;
}
return sum;
}
public static void main(String[] args) {
System.out.println("Hello, World!");
// System.out.println(sumOfDigits(16));
System.out.println("1234567890".matches("[0-9]{10}"));
System.out.println("123456789".matches("[0-9]{10}"));
}
}