私はこの問題を解決する方法を理解できないようです。チェックサムとコードがどのように見えるかを見つけることで、数学に行き詰まっています。
コードにいくつか問題があり、ずさんなように見え、おそらくもっと小さく、より簡潔な方法で記述できる可能性があることは知っていますが、私は配列に慣れていないため、配列を最適化することはまだ得意ではありません。
import java.util.Arrays;
import java.util.Scanner;
public class ISBN{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter ISBN:");
String isbnSTR = keyboard.nextLine();
String x;
int isbnARRAY[] = new int[10];
int total = 0;
for(int i = 0; i < isbnSTR.length(); i++){
x = ""+isbnSTR.charAt(i);
isbnARRAY[i] = Integer.parseInt(x);
total = isbnARRAY[i] * (10 - 1);
boolean isbnVALID = PassArray(isbnARRAY, i);
if (isbnVALID == true){
System.out.println("Your isbn number is valid.");
} else {
System.out.println("Your isbn number is NOT valid");
}
}
}
public static boolean PassArray(int isbnARRAY[], int total){
int result = 0;
int checkSum = 0;
int len = isbnARRAY.length;
for (int x = 0; x < len; x++){
result = (x * total);
checkSum = result % 11;
}
System.out.println(result);
boolean isbnVALID;
if (checkSum == 0){
isbnVALID=true;
} else {
isbnVALID=false;
}
return true;
}
}