第五章第三十六题(商业应用:检测ISBN)(Business application: checking ISBN)

第五章第三十六题(商业应用:检测ISBN)(Business application: checking ISBN)

  • **5.36(商业应用:检测ISBN)使用循环简化编程练习题3.9。
    **5.36(Business application: checking ISBN) Use loops to simplify Programming Exercise 3.9.
  • 参考代码:
package chapter05;

import java.util.Scanner;

public class Code_26 {
    public static void main(String[] args) {
        int isbn,checkSum = 0,divisor = 100000000;

        System.out.print("Enter the first 9 digits of an ISBN as integer: ");
        Scanner input = new Scanner(System.in);
        isbn = input.nextInt();

        for(int i = 0;i < 9;i++)
        {
            int dI = isbn / divisor % 10;
            checkSum += dI * (i+1);
            divisor /= 10;
        }
        checkSum %= 11;

        if(checkSum == 10)
            System.out.printf("The ISBN-10 number is %d%dX", isbn / 100000000, isbn % 100000000);
        else
            System.out.printf("The ISBN-10 number is %d%d%d", isbn / 100000000, isbn % 100000000, checkSum);
    }
}

  • 结果显示:
Enter the first 9 digits of an ISBN as integer: 456287241
The ISBN-10 number is 4562872411
Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值