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

示例

線上演示

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

更新於:2019年12月20日

6K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.