我对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;
}
}
Thx in advan ce,抱歉,如果这不是正确的格式;这也是我的第一#1后¯\ _(ツ)_ /¯
哇!我不知道Stackoverflow是如此有用。谢谢你的帮忙。 –
虽然我仍然有1个问题,如果问题是赋值给'null',那么为什么在'validateCreditCard()'方法中使用'string'时才会影响它,只有当它传递到'checkSum ()' –
因为在你的代码中你使用了'number'变量而不是'string'变量。号码是实际的信用卡号码。在'checkSum'方法中,相反,你传递了错误的字符串变量。很高兴为您提供帮助,如果答案有助于您考虑接受它。这就是stackoverflow的工作原理。 https://stackoverflow.com/help/someone-answers – amicoderozer