/*
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;
}
}
Introduction to Java Programming编程题9.32<检测ISBN>
最新推荐文章于 2021-10-01 17:53:21 发布