帐户验证公式图片。
嗨,
我正在尝试使用图片中附带的公式验证帐号。
帐号将以字符串形式显示。最后,我需要检查并返回正确的帐号 正确执行上述验证。
帐号会有所不同,但对于此示例,我们可以使用21412900192413进行测试。 我只是设法将每个字符串分别与int数据类型分开,以便更容易执行 计算。任何人都可以帮我完成此操作,以根据图片中附带的公式验证帐户 以上请?
到目前为止我的代码:
public static void main(String[] args) throws Exception {
System.out.println("test");
String strAccount = "21412900192413";
String fixedValues = "65432987654320";
//for account number
String sampleString = strAccount;
String[] stringArray = sampleString.split("");
int[] intArray = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
String numberAsString = stringArray[i];
intArray[i] = Integer.parseInt(numberAsString);
}
System.out.println("Number of integers: " + intArray.length);
System.out.println("The integers are:");
for (int number1 : intArray) {
System.out.println(number1);
}
//for fixed values
String sampleFixedValues = fixedValues;
String[] stringArray2 = sampleFixedValues.split("");
int[] intArray2 = new int[stringArray2.length];
for (int i = 0; i < stringArray2.length; i++) {
String numberAsString = stringArray2[i];
intArray2[i] = Integer.parseInt(numberAsString);
}
System.out.println("Number of fixed values: " + intArray2.length);
System.out.println("The fixed values are:");
for (int number2 : intArray2) {
System.out.println(number2);
}
}
答案 0 :(得分:0)
谢谢你们,我终于明白了。如果有更简单的方法可以让我知道。如果可能的话,让它变短。
public static void main(String[] args) throws Exception {
System.out.println("test");
boolean success = false;
String strAccount = "21412900192413";
String fixedValues = "65432987654320";
//for account number
String sampleString = strAccount;
String[] stringArray = sampleString.split("");
int[] intArray = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
String numberAsString = stringArray[i];
intArray[i] = Integer.parseInt(numberAsString);
}
System.out.println("Number of integers: " + intArray.length);
System.out.println("The integers are:");
for (int number1 : intArray) {
System.out.println(number1);
}
//for fixed values
String sampleFixedValues = fixedValues;
String[] stringArray2 = sampleFixedValues.split("");
int[] intArray2 = new int[stringArray2.length];
for (int i = 0; i < stringArray2.length; i++) {
String numberAsString = stringArray2[i];
intArray2[i] = Integer.parseInt(numberAsString);
}
System.out.println("Number of fixed values: " + intArray2.length);
System.out.println("The fixed values are:");
for (int number2 : intArray2) {
System.out.println(number2);
}
//for total
int totalSum = 0;
int[] total = new int[stringArray2.length];
for (int j = 0; j < stringArray2.length; j++) {
total[j] = intArray[j] * intArray2[j];
System.out.println("Total: " + total[j]);
totalSum = totalSum + total[j];
}
System.out.println("TotalSum: " + totalSum);
//Check Digit Formula = 10 – (Total Modulus 11 / Total%11)
int digitFormula = 10 - (totalSum % 11);
System.out.println("digitFormula: " + digitFormula);
int lastNumber = intArray[intArray.length-1];
System.out.println("lastNumber: " + lastNumber);
if(digitFormula == lastNumber){
success = true;
}
}
答案 1 :(得分:0)
public static void main(String args[]) throws Exception {
System.out.println("test");
String strAccount = "21412900192418";
String fixedValues = "65432987654320";
// for account number
String[] stringArray = strAccount.split("");
int[] intArray = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
String numberAsString = stringArray[i];
intArray[i] = Integer.parseInt(numberAsString);
}
System.out.println("Number of integers: " + intArray.length);
System.out.println("The integers are:");
for (int number1 : intArray) {
System.out.println(number1);
}
// for fixed values
String sampleFixedValues = fixedValues;
String[] stringArray2 = sampleFixedValues.split("");
int[] intArray2 = new int[stringArray2.length];
for (int i = 0; i < stringArray2.length; i++) {
String numberAsString = stringArray2[i];
intArray2[i] = Integer.parseInt(numberAsString);
}
System.out.println("Number of fixed values: " + intArray2.length);
System.out.println("The fixed values are:");
for (int number2 : intArray2) {
System.out.println(number2);
}
int total = calculateTotal(intArray, intArray2);
if (checkDigit(intArray, total)) {
System.out.println("Valid Account Number...");
} else {
System.out.println("Invalis Account Number...");
}
}
private static boolean checkDigit(int[] original, int total) {
int digit = 10 - (total % 11);
if (original[original.length - 1] == digit)
return true;
return false;
}
private static int calculateTotal(int[] original, int[] fixed) throws Exception {
if (original.length != fixed.length) {
throw new Exception("Account number length mismatch with Fixed Values.");
}
int total = 0;
for (int i = 0; i < original.length; i++) {
int multi = original[i] * fixed[i];
total += multi;
}
return total;
}
这是我在最后尝试的内容。希望这会有所帮助。
干杯!!!