首页 > 试题广场 >

( 商业:检查 ISBN-10 )ISBN-10(国际标准

[问答题]
 ( 商业:检查 ISBN-10 )ISBN-10(国际标准书号)以前是一个 10 位整数d1d2d3d4d5d6d7d8d9d10最 后的一位 d10。是校验和,它是使用下面的公式用另外 9个数计算出来的: (d1 x 1 + d2 x 2+  d3x 3+d4 x 4+d5 x 5+d6 x 6+本d7x 7+ d8 x 8+ d9 x 9)% 11 
如果校验和为 10, 那么按照ISBN-10 的习惯,最后一位应该表示为 X。编写程序,提示用 户输人前 9 个数,然后显示 10 位 ISBN (包括前面起始位置的 0 >。程序应该读取一个整数输入。 以下是一个运行示例:




public class Test {
	public static void main(String[] args){
		
		
		//程序说明:提示用户输入9位数的整数,计算并输出书号
		//提示用户输入
		System.out.print("Enter the first 9digits of an ISBN as integer:");
		
		Scanner input = new Scanner(System.in);
		
		long d = input.nextLong();
		//计算
		int d1 = (int)d / 100000000;
		int d2 = (int)d / 10000000 % 10;
		int d3 = (int)d / 1000000 % 10;
		int d4 = (int)d / 100000 % 10;
		int d5 = (int)d / 10000 % 10;
		int d6 = (int)d / 1000 % 10;
		int d7 = (int)d / 100 % 10;
		int d8 = (int)d / 10 % 10;
		int d9 = (int)d % 10;
		//显示结果
		int d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11;
		if(d10 == 10)
			System.out.println("The ISBN-10 number is " + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + "X");
		else
			System.out.println("The ISBN-10 number is " + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + d10);
    }
}

发表于 2020-02-23 15:59:35 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter the first 9 digits of an ISBN as integer: ");
		String str = sc.next();
		int sum = 0;
		for(int i = 0; i < str.length(); ++i) {
			sum += (i + 1) * (str.charAt(i) - '0');
		}
		String a = "The ISBN-10 number is ";
		if(sum % 11 == 10)
			System.out.println(a + str + 'X');
		else
			System.out.println(a + str + (sum % 11));
	}
}

发表于 2019-11-14 10:47:27 回复(0)