3.9(商业:检验ISBN-10)ISBN-10(国际标准书号)由10个个位整数\LARGE d_{1}d_{2}d_{3}d_{4}d_{5}d_{6}d_{7}d_{8}d_{9}d_{10}组成,最后一位\LARGE d_{10}是校验和,它是使用下面的公式用另外9个数计算出来的:
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9)%11
如果校验和为10,那么按照ISBN-10的习惯,最后一位应该表示为X。编写程序,提示用户输入前9个数,然后显示10位ISBN(包括前面起始位置的0)。程序应该读取一个整数输入。
以下是一个运行示例:
Enter the first 9 digits of an ISBN as integer:013601267
The ISBN-10 number is 0136012671
Enter the first 9 digits of an ISBN as integer:013031997
The ISBN-10 number is 013031997X
import java.util.Scanner;
public class TestISBN_10 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter the first 9 digitd of an ISBN as integer: ");
int d[]=new int[11];
for(int i=1;i<10;i++) {
d[i]=input.nextInt();
}
int D=d[1]+d[2]*2+d[3]*3+d[4]*4+d[5]*5+d[6]*6+d[7]*7+d[8]*8+d[9]*9;
int d10=(int)D%11;
if(d10==10) {
d10=88;
System.out.println("The ISBN-10 number is "+d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+d[7]+d[8]+d[9]+(char)d10);
}
else
System.out.println("The ISBN-10 number is "+d[1]+d[2]+d[3]+d[4]+d[5]+d[6]+d[7]+d[8]+d[9]+d10);
}
}