Luhn 算法的多个错误

Multiple errors on Luhn Algorithm

我正在尝试获取用户输入的信用卡号并将其放入 Luhn 算法中。当我 运行 代码时,我得到一个 conversion='i' 错误。我在拨打 'check.'

时也收到一个两位数的号码
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter a credit card number (enter a blank line to quit): ");
    String ccNumber=keyboard.nextLine();
    long length2 = String.valueOf(ccNumber).length();

    int length = toIntExact(length2);

    if (length == 0) {
        System.out.print("Goodbye!");
        System.exit(0);
    }
    if (length  != 16) {
        System.out.println("ERROR! Number MUST have exactly 16 digits");
        System.exit(0);
    }
    int check = ccNumber.charAt(15);

    int sum = 0;
    boolean alternate = false;
    int i = length-1;
    while (i >= 0)
    {
            int n = Integer.parseInt(ccNumber.substring(i, i + 1));
            if (alternate)
            {
                    n *= 2;
                    if (n > 9)
                    {
                            n = (n % 10) + 1;
                    }
            }
            sum += n;
            alternate = !alternate;
            i--;
    }
    if (sum % 10 == 0) {
        System.out.printf("Check number should be: %i",check);
        System.out.printf("Number is valid\n");
    }
    else {
        System.out.printf("Check number should be: %s", check+"\n");
        System.out.printf("Number is NOT valid\n");
    }
    }
}

替换这个

int check = ccNumber.charAt(15);

有了这个

int check = Character.getNumericValue(ccNumber.charAt(15));

System.out.printf("Check number should be: %i",check);

System.out.printf("Check number should be: %d\n",check);

System.out.printf("Check number should be: %s", check+"\n");

System.out.printf("Check number should be: %d\n", check);