多说无益,贴代码:
/** * 校验银行卡卡号 * * @param cardid * @return */ public static boolean checkbankcard(string cardid) { char bit = getbankcardcheckcode(cardid .substring(0, cardid.length() - 1)); return cardid.charat(cardid.length() - 1) == bit; } /** * 从不含校验位的银行卡卡号采用 luhm 校验算法获得校验位 * * @param noncheckcodecardid * @return */ public static char getbankcardcheckcode(string noncheckcodecardid) { int cardlenth = noncheckcodecardid.trim().length(); if (noncheckcodecardid == null || cardlenth == 0 || !noncheckcodecardid.matches("\\d+")) { throw new illegalargumentexception("不是银行卡的卡号!"); } char[] chs = noncheckcodecardid.trim().tochararray(); int luhmsum = 0; for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) { int k = chs[i] - '0'; if (j % 2 == 0) { k *= 2; k = k / 10 + k % 10; } luhmsum += k; } return (luhmsum % 10 == 0) ? '0' : (char) ((10 - luhmsum % 10) + '0'); }
感觉这个复制过去即可...所以我也没深入研究.感觉浪费时间.
以上所述是小编给大家介绍的java校验银行卡是否正确的核心代码,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
【java基础】面试常见问题:类和对象,封装继承多态,final关键字,static关键字,类加载过程,双亲委派模型
荐 Java语言基础之JDK1.8新特性(Lambda表达式、函数式接口、Stream流、新的日期API)
网友评论