// Version at the end of class - // Almost but not quite the solution!! See the handout // Determine the last digit of a credit card number // // Method for verifying a credit card number: // 1. Starting with the second digit from the right, // double every other digit // 2. Break into digits, and add the digits together // 3. Result must end with 0 (e.g 70) public class Credit { public static void main(String[] args) { String credit = "123456789"; int sum = 0; for (int i=credit.length()-1; i>=0; i=i-2) { char c = credit.charAt(i); int num = 2*Character.getNumericValue(c); sum = sum + num/10 + num%10; System.out.println("sum:" + sum + ", num:" + num); } for (int i=credit.length()-2; i>=0; i=i-2) { char c = credit.charAt(i); int num = Character.getNumericValue(c); sum = sum + num/10 + num%10; System.out.println("sum:" + sum + ", num:" + num); } System.out.println("last digit should be:" + (10-sum%10)); } }