【发布时间】:2018-01-18 17:25:23
【问题描述】:
我对 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 帖子¯\_(ツ)_/¯
【问题讨论】:
-
哇!我不知道 Stackoverflow 这么有用。非常感谢您的帮助。
-
我还有一个问题,如果问题是分配给
null,那么为什么在validateCreditCard()方法中使用string时不影响它,只有当它是传入checkSum() -
因为在您评论的代码中您使用了
number变量而不是string变量。 number 是实际的信用卡号。相反,在checkSum方法中,您传递了错误的字符串变量。很高兴为您提供帮助,如果答案对您有帮助,请考虑接受。这就是stackoverflow的工作原理。 stackoverflow.com/help/someone-answers -
并且下次您可以直接在答案下方发表评论,以便回答的人会收到您的消息通知。如果您在此处在您的问题下发表评论,您必须用
@username标记此人,如果您希望他收到您的消息通知。 meta.stackexchange.com/questions/125208/… -
感谢 stackoverflow 提示。我会在你的答案下发表评论
标签: java methods logic credit-card