我對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