Created
January 22, 2019 06:55
-
-
Save asd1245dss/b8fe7424277054ea4af774782e0dd457 to your computer and use it in GitHub Desktop.
基于luhn算法实现的银行卡号校验
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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