问题描述:

I'm working on a school assignment that checks whether a credit card number that is entered is valid or not, using Luhn's Algorithm.

In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388 5760 1840 2626):

  1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.

  2. Now add all single-digit numbers from Step 1: 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

  3. Add all digits in the odd places from right to left in the card number: 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

  4. Sum the results from Step 2 and Step 3: 37 + 38 = 75

  5. If the result from Step 4 is divisible by 10 the card number is valid; otherwise, it is invalid. For example, the number 4388 5760 1840 2626 is invalid, but the number 4388 5760 1841 0707 is valid.

I need to write this program using the methods in the code I have written:

public class CreditCardValidation {

public static void main(String[] args, long input) {

Scanner numberinput = new Scanner(System.in);

System.out.println("Enter a credit card number as a long integer: ");

long cardnumber = numberinput.nextLong();

if (isValid(input) == true) {

System.out.println(numberinput + " is valid.");

} else {

System.out.println(numberinput + " is invalid.");

}

}

public static boolean isValid(long number){

int total = sumOfDoubleEvenPlace + sumOfOddPlace;

return (total % 10 == 0) && (prefixMatched(number, 1) == true) &&

(getSize(number)>=13) && (getSize(number)<=16);

}

public static int sumOfDoubleEvenPlace(long number) {

int doubledevensum = 0;

long place = 0;

while (number > 0) {

place = number % 100;

doubledevensum += getDigit((int) (place / 10) * 2);

number = number / 100;

}

return doubledevensum;

}

public static int sumOfOddPlace(long number) {

int oddsum = 0;

while (number <= 9) {

oddsum += (int)(number % 10);

number = number % 100;

}

return oddsum;

}

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 boolean prefixMatched(long number, int d) {

if ((getPrefix(number, d) == 4)

|| (getPrefix(number, d) == 5)

|| (getPrefix(number, d) == 3)) {

if (getPrefix(number, d) == 3) {

System.out.println("\nVisa Card ");

} else if (getPrefix(number, d) == 5) {

System.out.println("\nMaster Card ");

} else if (getPrefix(number, d) == 3) {

System.out.println("\nAmerican Express Card ");

}

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;

}

}

}

I just started learning how to program two months ago so I am fairly new to this. The program doesn't compile and I don't know why and what I have to do to fix this. I know there are similar topics already posted regarding this and I have been using this post to help guide me a bit. Can someone help point a student in the right direction and let me know what I'm doing wrong?

网友答案:

Your program isn't compiling because this line:

int total = sumOfDoubleEvenPlace + sumOfOddPlace;

since sumOfDoubleEvenPlace and sumOfOddPlace are functions, you must use them as such:

int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
网友答案:

In the function isValid you are trying to add two variables which do not exist. However you have defined them as functions and to use them as functions you must call them as functions using

int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number); 
相关阅读:
Top