Introduction to Java Programming编程题9.32<检测ISBN>

本文介绍了一个简单的Java程序,用于计算图书ISBN号的第10位校验码。程序首先验证输入是否仅包含数字且长度不超过9位,然后通过特定算法计算并输出校验码。
/*
Enter the ISBN numbers of the book: 013601267
0136012671
*/
import java.util.Scanner;

public class CheckISBN1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the ISBN numbers of the book: ");
        String s = input.next();
        for (int i = 0; i < s.length(); i++) {
            if (!Character.isDigit(s.charAt(i)) || s.length() > 9) {
                System.out.println("The string must be numbers.");
                System.exit(0);
            }
        }
        s = computeTenth(s);

        System.out.println(s);
    }

    public static String computeTenth(String s) {
        StringBuilder stringBuilder = new StringBuilder(s);
        int[] isbn = new int[9];
        int Ten = 0;

        for (int i = 0; i < s.length(); i++) {
            isbn[i] = charToInt(s.charAt(i));
            Ten += isbn[i] * (i + 1);
        }
        Ten %= 11;
        if (Ten == 10)
            stringBuilder.append('X');
        else
            stringBuilder.append(Ten);

        return stringBuilder.toString();
    }

    public static int charToInt(char ch) {
        return ch - 48;
    }
}
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1756793511672" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9306" width="68" height="68" xmlns:xlink="http://www.w3.org/1999/xlink"> <path d="M928 222v622.8a83.19 83.19 0 0 1-83.2 83.2H222A126 126 0 0 1 96 802V222A125.93 125.93 0 0 1 222 96h580a126 126 0 0 1 126 126z" fill="#03A9F5" p-id="9307"></path> <path d="M731.1 928H222A126 126 0 0 1 96 802V370q12.42-0.5 25-0.5 15.06 0 29.93 0.72C445.31 384.42 685.22 606.47 727 892.83q2.55 17.43 4.1 35.17z" fill="#FFFFFF" opacity=".2" p-id="9308"></path> <path d="M928 222v622.8a82.87 82.87 0 0 1-6.69 32.74A635.16 635.16 0 0 1 774.7 894.6q-24.06 0-47.69-1.77c-291-21.67-527.21-240-576.13-522.58a638.8 638.8 0 0 1 8.24-257.47A125.48 125.48 0 0 1 222 96h580a126 126 0 0 1 126 126z" fill="#FFFFFF" opacity=".14" p-id="9309"></path> <path d="M804.61 636.17H778a7.08 7.08 0 0 0-3.47 1l-12-36.43c-3.84-20.71-18.28-30.7-36-30.7H614c-20.38 0-32.75 13.41-36 30.69l-12 36.54a7.24 7.24 0 0 0-3.68-1.06h-26.58a7.44 7.44 0 0 0-7.4 7.47v6.49a7.44 7.44 0 0 0 7.4 7.47l10.07 1.73a47.3 47.3 0 0 0-5 21.78l-3.72 43a30.81 30.81 0 0 0 0.25 3.8 13.16 13.16 0 0 0-0.25 2.42V782a13.27 13.27 0 0 0 13.21 13.32h20.16A13.26 13.26 0 0 0 583.65 782v-16.6h173.28V782a13.27 13.27 0 0 0 13.21 13.32h20.16A13.26 13.26 0 0 0 803.5 782v-51.69a13.5 13.5 0 0 0-0.24-2.42 35.24 35.24 0 0 0 0.24-3.8l-3.73-43a47.3 47.3 0 0 0-5-21.83l9.85-1.68a7.43 7.43 0 0 0 7.39-7.47v-6.49a7.42 7.42 0 0 0-7.39-7.47z m-217.72-7.65l8.8-23.64 0.12-0.55c0.88-4.78 2.84-5.63 5.72-9.32h137.7c2.93 3.8 4.74 4.76 5.54 9.27l8.93 24.23 3.3 13.17c-0.73 9.64-12.2 17.25-21.75 17.25H605.38c-9.55 0-21-7.61-21.77-17.25z m6.17 112.38a20.19 20.19 0 1 1 20-20.18 20.08 20.08 0 0 1-20 20.18zM704.74 726a6 6 0 0 1-6 6h-56.05a6 6 0 0 1-5.94-6v-16.87a6 6 0 0 1 5.94-6h56.09a6 6 0 0 1 6 6V726z m42.8 14.89a20.19 20.19 0 1 1 20-20.18 20.09 20.09 0 0 1-20 20.19z" fill="#FFFFFF" p-id="9310"></path> <path d="M520.42 702.93c0.08-2.94 0.28-5.88 0.6-8.8l3.4-30.85-2.17-0.28A11.28 11.28 0 0 1 511 651.75v-14.5A11.28 11.28 0 0 1 522.25 626l23.54 2 15.28-43.16c1.32-3.54 3.62-9.43 5.21-12.85C570 564 576 559 586 557h91.94l-0.47-232.09a52.15 52.15 0 0 0-52-52h-304a52.15 52.15 0 0 0-52 52v363a52.15 52.15 0 0 0 52 52h198z m-189.95-313a20.06 20.06 0 0 1 20-20h212a20.06 20.06 0 0 1 20 20 20.06 20.06 0 0 1-20 20h-212a20.06 20.06 0 0 1-20-20z m3.69 100.36a18.41 18.41 0 0 1 18.36-18.36h175.59a18.41 18.41 0 0 1 18.36 18.36v3.28a18.41 18.41 0 0 1-18.36 18.36H352.52a18.41 18.41 0 0 1-18.36-18.36z m157.31 108.26a18.41 18.41 0 0 1-18.36 18.36H353.52a18.41 18.41 0 0 1-18.36-18.36v-3.28a18.41 18.41 0 0 1 18.36-18.36h119.59a18.41 18.41 0 0 1 18.36 18.36z" fill="#FFFFFF" p-id="9311"></path> </svg> 以上代码中svg中图片周边空白区域太多了,如何缩小空白区域50%
最新发布
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值