我是java的新手,我试图尽可能地破坏我的代码。这个问题实际上是关于如何组织方法一起工作
如果使用checkSum()
方法编写validateCreditCard()
代码,我的信用卡验证工作正常。我觉得这很奇怪'因为它被checkDigitControl()
方法调用
我将这些来源用于程序的逻辑:
要检查〜https://www.creditcardvalidator.org/articles/luhn-algorithm
生成〜https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm
这是我的代码(如果它相当笨拙,我提前道歉)
public class CreditCards {
public static void main(String[] args) {
long num;
num = genCreditCard();
boolean bool = validateCreditCard(num);
}
// Validity Check
public static boolean validateCreditCard(long card) {
String number = card+"";
String string=null;
int i;
for(i=0; i<number.length()-1; i++) {//Populate new string, leaving out last digit.
string += number.charAt(i)+"";
}
String checkDigit = number.charAt(i)+"";// Stores check digit.
long sum = checkSum(string);// Program works if this line is swapped for the code below(from checkSum)
//**********************************************************************
// int[] digits = new int[number.length()];
// int lastIndex = digits.length-1;
// int position=2; int mod=10;
// int sum=0;
//
// for(int j=lastIndex; j>=0; j--) {// Populate array in REVERSE
// digits[j] = Integer.parseInt(number.charAt(j)+"");
// digits[j] *= ( (position%2 == 0) ? 2: 1 );// x2 every other digit FROM BEHIND
// position++;
//
// digits[j] = ( (digits[j] > 9) ? (digits[j] / mod)+(digits[j] % mod) : digits[j] );//Sums integers of double-digits
// sum += digits[j];
// }
//**********************************************************************
sum *= 9;
string = sum+"";
string = string.charAt(string.length()-1)+"";// Last digit of result.
return (string.equals(checkDigit));
}
public static long genCreditCard() {
String number = "34";// American Express(15 digits) starts with 34 or 37
for(int i=0; i<12; i++)
number += (int)(Math.random() * 10) + "";// Add 12 random digits 4 base.
number += checkDigitControl(number);// Concat the check digit.
System.out.println(number);
return Long.parseLong(number);
}
// Algorithm to calculate the last/checkSum digit.
public static int checkDigitControl(String number) {
int i;
for(i=0; i<5; i++)
++i;
int sum = checkSum(number);
return 10 - sum%10;// Returns number that makes checkSum a multiple of 10.
}
public static int checkSum(String number) {
int[] digits = new int[number.length()];
int lastIndex = digits.length-1;
int position=2; int mod=10;
int sum=0;
for(int j=lastIndex; j>=0; j--) {// Populate array in REVERSE
digits[j] = Integer.parseInt(number.charAt(j)+"");
digits[j] *= ( (position%2 == 0) ? 2: 1 );// x2 every other digit FROM BEHIND
position++;
digits[j] = ( (digits[j] > 9) ? (digits[j] / mod)+(digits[j] % mod) : digits[j] );//Sums integers of double-digits
sum += digits[j];
}
return sum;
}
}
提前谢谢,如果这不是正确的格式,请对不起;它也是我的第一个Stackoverflow帖子¯\ _(ツ)_ /¯
答案 0 :(得分:0)
您正在使用空值初始化变量字符串:
String string=null;
以下是您将卡号的每个字符添加到此字符串中。
for(i=0; i<number.length()-1; i++) {
string += number.charAt(i)+"";
}
但这会导致变量字符串为null + cardnumbers
,因为您没有初始化字符串string
,并且值null被转换为字符串"null"
({{ 3}})
这将修复您的代码:
String string = new String();
注意,这段代码:
for(i=0; i<number.length()-1; i++) {
string += number.charAt(i)+"";
}
可以很容易地被这行做同样的事情取代:
number = number.substring(0, number.length() -1);
如果您切换到此代码,只需将number
传递给checkSum
方法