Java语言程序设计 例题3.9(检验ISBN)

**3.9(Business: check ISBN-10) An ISBN-10 (International Standard Book Number) 
consists of 10 digits: d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, 
which is calculated from the other nine digits using the following formula:
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 +d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11
If the checksum is 10, the last digit is denoted as X according to the ISBN-10 
convention. Write a program that prompts the user to enter the first 9 digits and 
displays the 10-digit ISBN (including leading zeros). Your program should read 
the input as an integer. Here are sample runs:
VideoNote
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
**3.9(商业:检验ISBN-10)ISBN-10国际书写号由10位数字组成 d1 d2 d3 d4 d5 d6 d7 d8 d9 d10,最后一位d10是校验和,它是使用下面的公式用另外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 Unite3Test9 
{
	public static void main(String[] args) 
	{
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入前九位数字");
		int d1 = scan.nextInt();
		int d2 = scan.nextInt();
		int d3 = scan.nextInt();
		int d4 = scan.nextInt();
		int d5 = scan.nextInt();
		int d6 = scan.nextInt();
		int d7 = scan.nextInt();
		int d8 = scan.nextInt();
		int d9 = scan.nextInt();
		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.print("The ISBN-10 number is "+d1);
			System.out.print(d2);
			System.out.print(d3);
			System.out.print(d4);
			System.out.print(d5);
			System.out.print(d6);
			System.out.print(d7);
			System.out.print(d8);
			System.out.print(d9);
			System.out.print("x");
		}else
		{
			System.out.print("The ISBN-10 number is "+d10);
			System.out.print(d2);
			System.out.print(d3);
			System.out.print(d4);
			System.out.print(d5);
			System.out.print(d6);
			System.out.print(d7);
			System.out.print(d8);
			System.out.print(d9);
			System.out.print(d10);
		}
		
		
	}

}

结果如下:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值