Skip to content

Instantly share code, notes, and snippets.

@asd1245dss
Created January 22, 2019 06:55
Show Gist options
  • Save asd1245dss/b8fe7424277054ea4af774782e0dd457 to your computer and use it in GitHub Desktop.
Save asd1245dss/b8fe7424277054ea4af774782e0dd457 to your computer and use it in GitHub Desktop.
基于luhn算法实现的银行卡号校验
package com.extracme.evcard.eye.common;
/**
* @version 2019-01-22 09:31
*/
public class CardValidation {
private static boolean checkBankCardNo(String cardNo) {
char[] cartNoArray = cardNo.toCharArray();
int cardNoLength = cartNoArray.length;
int total = 0;
for (int i = 1; i < cardNoLength; i++) {
int j = cardNoLength - 1 - i;
int num = cartNoArray[j] - 48;
if ((i + 1) % 2 == 0) {
num += num;
if (num > 10) {
num = (num - 10) + 1;
}
}
total += num;
}
System.out.printf("%s %s", total, total % 10);
return (total + cardNo.charAt(cardNoLength - 1) - 48) % 10 == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment