Package Cn.dlpu.lby;
Import Java.util.Scanner; public class Xinyongkahaoma {/* When you enter your credit card number, are you worried about losing the wrong one?
You don't have to worry so much, because not a casual credit card number is legal, it has to be verified through the Luhn algorithm.
The verification process: 1, starting from the last digit of the card number, the reverse adds odd digits (1, 3, 5, and so on).
2, starting from the last digit of the card number, the inverse of the even digit number, first multiplied by 2 (if the product is two digits, then subtract 9), and then sum.
3, the sum of odd digits plus even digits sum, the result should be divisible by 10.
For example, the card number is: 5432123456788881 odd, even digits (marked in red) Distribution: 5432123456788881 odd digits and =35 even digits multiplied by 2 (some to subtract 9) Result: 1 6 2 6 1 5 7 7, sum = 35.
Finally, the 35+35=70 can be divisible by 10, the verification pass. Please write a program, enter the card number from the keyboard, and then determine whether the check passes.
"Failed" is displayed by the display: "Success". For example, user input: 356827027232780 program output: Success */public static void main (string[] args) {//TODO auto-generated method stub Sca
Nner sc = new Scanner (system.in);
String str = sc.nextline ();
char[] cc = Str.tochararray ();
Cal (CC); }//Save string into array private static void Cal (char[] cc) {//TODO auto-generated method stub int[] n = new Int[cc.length+1]
;
int j = 1;
for (int i = cc.length-1;i>=0;i--) {n[j++] = cc[i]-' 0 '; } if (Test (n)) System.out.println ("success.
"); Else System.out.println ("failed.
"); }
//judge whether the private static Boolean test (int[] n) {//TODO auto-generated method stub int even = 0;
int odd = 0;
for (int i = 1;i<n.length;i++) {if (i%2 = = 0) {int temp = n[i]*2;
if (temp<10) {even + = temp;
} else{temp = temp-9;
even + = temp;
}} else{Odd + = N[i];
}} int total = even + odd;
if (total%10 = = 0) return true;
return false;
}
}