alternate
//声明变量
import java.util.Scanner;
//检查isbn数字的长度
public class ISBNChecker {
public static void main(String [] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a 13 digit book number:");
String book = keyboard.nextLine();
int isbn = Integer.parseInt(book);
//检查每1,3,5,7,9,11和13号的数字是否乘以3,每隔一个数字乘以1,然后将它们加在一起
if (book.length() != 13) {
System.out.println("ILLEGAL ISBN NUMBER");
}
}
for(int i = 0; i < book.length(); i++){
char c = book.charAt(i); //get the current char
int value = c - '0'; //subtract '0' from the char
isbn += value * (i+1);
//the rest is to see if total of isbn is dividable by 10 to have no remains
}
if ((isbn % 10) != 0) {
System.out.println("Illegal ISBN number");
} else {
System.out.println("It's an ISBN number");
}
}
//the problem with this code is that it won't work and I am pretty sure I messed up the format. Please help me check.
// when I process it. this error shows up :
答案 0 :(得分:2)
这个数字太大而无法融入整数。请尝试使用Long
代替..
Max Integer是2147483647。
Max Long是9223372036854775807L。