Java信用卡号码验证程序
给定一个包含信用卡号码数字的长数字;任务是使用程序查找信用卡号码是否有效。
为了检查信用卡是否有效,我们需要确保以下验证才能声明结果。
信用卡号码必须有13到16位数字,必须以以下数字开头。
- 所有Visa卡都以4开头
- 所有万事达卡都以5开头
- 美国运通卡以37开头
- 所有Discover卡都以6开头
检查信用卡是否有效的步骤:
步骤1 - 从右到左,我们将每位数字加倍,如果加倍结果是一位数,则保留原样,否则将两位数相加得到一位数。(例如22 = 2+2= 4)
步骤2 - 从信用卡号码的右到左,将所有奇数位的数字相加。
步骤3 - 将步骤1中得到的所有一位数相加。
步骤4 - 将步骤2和步骤3的结果相加。
步骤5 - 如果结果能被10整除,则信用卡号码有效,否则无效。
示例
Input: n = 4440967484181607 Output: 4440967484181607 is valid Input: n = 379354508162306 Output: 379354508162306 is valid
我们用来解决问题的方案 -
我们将使用Luhn校验或模10校验,对于数字4440967484181607。
算法
Start Step1-> In function void main(String[] args) Declare and initialize cnumber = 4440967484181607L Call function validitychk Print the result Step 2-> In function boolean validitychk(long cnumber) Return thesize(cnumber) >= 13 && thesize(cnumber) <= 16) && (prefixmatch(cnumber, 4) || prefixmatch(cnumber, 5) || prefixmatch(cnumber, 37) || prefixmatch(cnumber, 6)) && ((sumdoubleeven(cnumber) + sumodd(cnumber)) % 10 == 0 Step 3-> In function int sumdoubleeven(long cnumber) Declare and set sum = 0 Declare and set num = cnumber + "" Loop For i = thesize(cnumber) – 2 and i >= 0 and i -= 2 Set sum = sum + getDigit(Integer.parseInt(num.charAt(i) + "") * 2) Return sum Step 4-> In function int getDigit(int cnumber) if cnumber < 9 then, Return cnumber Return cnumber / 10 + cnumber % 10 Step 5-> In function int sumodd(long cnumber) Set sum = 0 Set num = cnumber + "" Loop For i = thesize(cnumber) – 1 and i >= 0 and i -= 2 Set sum = sum + Integer.parseInt(num.charAt(i) + "") Return sum Step 6-> In function boolean prefixmatch(long cnumber, int d) Return getprefx(cnumber, thesize(d)) == d Step 7-> In function int thesize(long d) Set num = d + "" Return num.length() Step8-> In function long getprefx(long cnumber, int k) If thesize(cnumber) > k then, Set num = cnumber + "" Return Long.parseLong(num.substring(0, k)) Return cnumber Stop
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
示例
import java.util.Scanner; public class Main { // Main Method public static void main(String[] args) { long cnumber = 4440967484181607L; System.out.println(cnumber + " is " + (validitychk(cnumber) ? "valid" : "invalid")); } // Return true if the card number is valid public static boolean validitychk(long cnumber) { return (thesize(cnumber) >= 13 && thesize(cnumber) <= 16) && (prefixmatch(cnumber, 4) || prefixmatch(cnumber, 5) || prefixmatch(cnumber, 37) || prefixmatch(cnumber, 6)) && ((sumdoubleeven(cnumber) + sumodd(cnumber)) % 10 == 0); } // Get the result from Step 2 public static int sumdoubleeven(long cnumber) { int sum = 0; String num = cnumber + ""; for (int i = thesize(cnumber) - 2; i >= 0; i -= 2) sum += getDigit(Integer.parseInt(num.charAt(i) + "") * 2); return sum; } // Return this cnumber if it is a single digit, otherwise, // return the sum of the two digits public static int getDigit(int cnumber) { if (cnumber < 9) return cnumber; return cnumber / 10 + cnumber % 10; } // Return sum of odd-place digits in cnumber public static int sumodd(long cnumber) { int sum = 0; String num = cnumber + ""; for (int i = thesize(cnumber) - 1; i >= 0; i -= 2) sum += Integer.parseInt(num.charAt(i) + ""); return sum; } // Return true if the digit d is a prefix for cnumber public static boolean prefixmatch(long cnumber, int d) { return getprefx(cnumber, thesize(d)) == d; } // Return the number of digits in d public static int thesize(long d) { String num = d + ""; return num.length(); } // Return the first k number of digits from // number. If the number of digits in number // is less than k, return number. public static long getprefx(long cnumber, int k) { if (thesize(cnumber) > k) { String num = cnumber + ""; return Long.parseLong(num.substring(0, k)); } return cnumber; } }
输出
4440967484181607 is valid
广告