验证信用卡号

时间:2019-03-26 07:48:42

标签: java

我当时正在用Java做一个作业程序,我把它弄醒了,但是在尝试进行while循环以使程序要求输入直到我键入-1时遇到了问题。我似乎无法让该程序继续要求输入,将不胜感激,如果您发现我的代码中有任何错误,请告诉我。

import java.util.Scanner;

public class Creditcard {
    // Return true if the card number is valid
    public static boolean isValid(long number) {

        int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);


        if ((total % 10 == 0) && (prefixMatched(number, 1) == true) && (getSize(number)>=13 )
                && (getSize(number)<=16 )) {
            return true;
        } else {
            return false;
        }
    }

    public static int getDigit(int number) {

        if (number <= 9) {
            return number;
        } else {
            int firstDigit = number % 10;
            int secondDigit = (int) (number / 10);

            return firstDigit + secondDigit;
        }
    }
    public static int sumOfOddPlace(long number) {
        int result = 0;

        while (number > 0) {
            result += (int) (number % 10);
            number = number / 100;
        }

        return result;
    }

    public static int sumOfDoubleEvenPlace(long number) {

        int result = 0;
        long temp = 0;

        while (number > 0) {
            temp = number % 100;
            result += getDigit((int) (temp / 10) * 2);
            number = number / 100;
        }

        return result;
    }

    public static boolean prefixMatched(long number, int d) {

        String card; 
        if ((getPrefix(number, d) == 4) 
                || (getPrefix(number, d) == 5)
                || (getPrefix(number, d) == 6)
                || (getPrefix(number, d) == 37)) {

            if (getPrefix(number, d) == 4) {
                card = ("Visa ");
            } else if (getPrefix(number, d) == 5) {
                card = ("Master");
            } else if (getPrefix(number, d) == 6) {
                card = ("Discover Card ");
            } else if (getPrefix(number, d) == 37) {
                card = ("American Express cards");
            }

            return true;

        } else {

            return false;

        }
    }

    public static int getSize(long d) {

        int count = 0;

        while (d > 0) {
            d = d / 10;

            count++;
        }

        return count;

    }

    public static long getPrefix(long number, int k) {

        if (getSize(number) < k) {
            return number;
        } else {

            int size = (int) getSize(number);

            for (int i = 0; i < (size - k); i++) {
                number = number / 10;
            }

            return number;

        }

    }

    public static void main(String[] args) {

        long number;
        // Create scanner object
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a credit card number: ");

        number=input.nextLong();
        if (isValid(number)) {
            String card;

        long n=getPrefix(number,1);

        if (n == 4)
            card = "Visa ";

        else if (n == 5)
            card = "Master ";

        else if (n == 6)
            card = "Discovery ";

        else
            card = "American Express ";

       System.out.print("This is a valid "+card+"Card Number");
    }
    else
    System.out.print("This is Invalid Credit Card Number");
    }
} 

我遇到的问题是,直到我按-1时,我才能反复要求刷卡

Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book)

Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with:

4 for Visa cards
5 for Master cards
6 for Discover cards
37 for American Express cards
The algorithm for determining whether a card number is entered correctly as developed by Hans Luhn of IBM in 1954. The Luhn check or Mod 10 check works as follows. Consider the card number 5842016792622547.

Double every second digit from right to left. If the doubling of a digit results in a two-digit number, add up the digits to get a single-digit number. The digits are shown in bold and italic here:
5842016792622547

This gives us (from right to left):

4 * 2 = 8
2 * 2 = 4
6 * 2 = 12 → (1 + 2) = 3
9 * 2 = 18 → (1 + 8) = 9
6 * 2 = 12 → (1 + 2) = 3
0 * 2 = 0
4 * 2 = 8
5 * 2 = 10 → (1 + 0) = 1
Add up those numbers (8 + 4 + 3 + 9 + 3 + 0 + 8 + 1 = 36)
Now add up all the digits in the odd positions from right to left: (7 + 5 + 2 + 2 + 7 + 1 + 2 + 8 = 34)
Add those two numbers (36 + 34 = 70). If this sum is divisible by 10, the number is valid. If it is not divisible by 10, the number is not valid.
Your program will prompt the user to enter a credit card number as a long integer and display whether the number is valid or not. Design your program to use the following methods:

Return true if the card number is valid */
public static boolean isValid(long number)

Return the sum of the doubled even-place digits */
public static int sumOfDoubleEvenPlace(long number)

Return the given number if it is a single digit,
otherwise return the sum of the two digits */
public static int getDigit(int n)

Return the sum of the odd-place digits */
public static int sumOfOddPlace(long number)

Return the number of digits in the given number */
public static int getSize(long number)

Return the first length_prefix number of digits from number. If the number of digits in number is less than length_prefix, return number. */
public static long getPrefix(long number, int length_prefix)
You may also implement this function if you feel it is useful for your solution:

Return true if prefix is a prefix for number */
public static boolean prefixMatched(long number, int prefix)
Here is the result of running the program a few times:

Enter credit card number: 412345678
That is not a valid credit card number.
Enter credit card number: 5842016792428358
That is a valid credit card number.
Enter credit card number: 5782077482835719
That is not a valid credit card number.

Extra Challenges
Challenge 1: If the card is valid, display which type of card it is. For example:

Enter credit card number: 5842016792428358
That is a valid Master card number.
Challenge 2: Repeatedly ask for credit card numbers until the user enters -1. (Hint: while)

0 个答案:

没有答案