Java黑皮书课后题第5章:*5.47(商业:检测ISBN-13)ISBN-13是标识书籍的新标准。它使用13位数字d1d2d3~d12d13,d13是校验和。如果校验和为10,则替换为0。求所有数字
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第5章:*5.47(商业:检测ISBN-13)ISBN-13是标识书籍的新标准。它使用13位数字d1d2d3~d12d13,d13是校验和。如果校验和为10,则替换为0。求所有数字
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
5.47(商業(yè):檢測ISBN-13)ISBN-13時標識書籍的新標準。它使用13位數(shù)字d1d2d3~d12d13,d13是校驗和。讀入前12位輸出全部位數(shù)
- 題目
- 題目概述
- 運行示例
- 破題
- 代碼
題目
題目概述
5.47(商業(yè):檢測ISBN-13)ISBN-13時標識書籍的新標準。它使用13位數(shù)字d1d2d3d4d5d6d7d8d9d10d11d12d13,最后一位數(shù)字d13是校驗和,是使用下面的公式從其他數(shù)字中計算出來的:
10 - (d1 + 3 * d2 + d3 + 3 * d4 + d5 + 3 * d6 + d7 + 3 * d8 + d9 + 3 * d10 + d11 + 3 * d12) % 12
如果校驗和為10,則替換為0。程序應(yīng)該將輸入作為一個字符串讀入。
運行示例
Enter the first 12 digits of an ISBN-13 as a string: 978013213080 The ISBN-13 number is 9780132130806 Enter the first 12 digits of an ISBN-13 as a string: 978013213079 The ISBN-13 number is 9780132130790 Enter the first 12 digits of an ISBN-13 as a string: 97801320 97801320 is an invalid input破題
代碼
import java.util.Scanner;public class Test5_47 {public static void main(String[] args) {// 1. 用string類型讀入前12位數(shù)字Scanner input = new Scanner(System.in);System.out.print("Enter the first 12 digits of an ISBN-13 as a string: ");String str = input.nextLine();// 判斷長度是否符合要求int length = str.length();if (length < 9 ) {System.out.println(str + " is an invalid input");System.exit(1);}// 2. 轉(zhuǎn)化為int類型long user_input = Long.parseLong(str);// 3. 取出每位數(shù)字int temp1 = 0, temp2 = 0, result = 0;for ( long i = 10000000000L ; i >= 1 ; i /= 100){temp1 = (int) (user_input / (i * 10));temp2 = (int) (user_input % (i * 10) / i);result += temp1 + 3 * temp2;user_input %= i;}// 4. 計算d13,如果是10則替換為0result = 10 - result % 10;if (result == 10)result = 0;// 5. 輸出結(jié)果System.out.print("The ISBN-13 number is " + str + result);} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第5章:*5.47(商业:检测ISBN-13)ISBN-13是标识书籍的新标准。它使用13位数字d1d2d3~d12d13,d13是校验和。如果校验和为10,则替换为0。求所有数字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第5章:*5.46(
- 下一篇: Java黑皮书课后题第5章:*5.48(