Return 'X' 9 位除以 11 的和为 10

Return 'X' when sum of 9 digits divided by 11 is 10

在我的代码中,我希望charSum到return'X'如果当9位数字之和除以9时余数是10。我都试过了charSum = 'X'charSum = (char) 88 都不起作用。我的算法中一定有什么地方错了。请帮忙。

public static char getCheckSum(String isbn) {
    int sum = 0;

    for (int i = 0; i < isbn.length(); i++) {
        int[] num = new int[isbn.length()]; 
        num[i] = Character.getNumericValue(isbn.charAt(i));
        sum = sum + num[i];
    }
    int last = (sum % 11);
    char charSum;
    if (last == 10){
        charSum = 'X';
    } else {
        charSum = (char) (last + 48);
    }
    return charSum;
}

public static String formatISBNWithHyphens(String isbn) {
    // original isbn:       123456789
    // possible new isbn:   1-234-56789-X
    char isbn10 = getCheckSum(isbn);
    String isbn10Str = isbn + Character.toString(isbn10);

//  char[] c = new char[isbn10Str.length()];    *leaving this here for future learning.
    String[] cStr = new String[isbn10Str.length()];
    String isbnStr = "";
    for (int i = 0; i < isbn10Str.length(); i++){
        cStr[i] = Character.toString(isbn10Str.charAt(i));
//      c[i] = isbn10Str.charAt(i);             *leaving this here for future learning.
        if (i == 0 || i == 3 || i == 8 ) {
            cStr[i] += '-';
         }

         isbnStr += cStr[i];
    }
    return isbnStr;
}

它工作正常。如果我 运行 它与 933456789 (其总和为 54,因此 54 % 11 = 10),getCheckSum() 方法 returns X 如预期的那样。

但是,这似乎不是计算 ISBN-10 校验和的正确方法。根据Wikipedia:

The 2001 edition of the official manual of the International ISBN Agency says that the ISBN-10 check digit – which is the last digit of the ten-digit ISBN – must range from 0 to 10 (the symbol X is used for 10), and must be such that the sum of all the ten digits, each multiplied by its (integer) weight, descending from 10 to 1, is a multiple of 11.

我按照规范实现了如下:

public static char getCheckDigit(String isbn) {
    if (isbn == null || !isbn.matches("[0-9]{9,}")) {
        throw new IllegalArgumentException("Illegal ISBN value");
    }

    int sum = 0;

    for (int i = 0; i < 9; i++) {
        sum += ((10 - i) * Character.digit(isbn.charAt(i), 10));
    }

    int check = ((11 - (sum % 11)) % 11);

    return check == 10 ? 'X' : Character.forDigit(check, 10);
}

应用于我在同一维基百科页面上找到的几个 ISBN 值:

getCheckDigit("097522980"); // --> returns 'X'
getCheckDigit("094339604"); // --> returns '2'
getCheckDigit("999215810"); // --> returns '7'
public class HelloWorld{

public static char getCheckSum(String isbn) {
    int sum = 0;

    for (int i = 0; i < isbn.length(); i++) {
        int[] num = new int[isbn.length()]; 
        num[i] = Character.getNumericValue(isbn.charAt(i));
        System.out.println(num[i]);
        sum = sum + num[i];
        System.out.println(sum);
    }
    int last = (sum % 11);
    char charSum;
    if (last == 10){
        charSum = 'X';
    } else {
        charSum = (char) (last + 48);
    }
    return charSum;
}


     public static void main(String []args){
          String isbn="123456787";
    // possible new isbn:   1-234-56789-X
    char isbn10 = getCheckSum(isbn);

        System.out.println(isbn10);


     }
}

它工作正常:)