0

帐户验证公式图片。

账户验证公式图片

你好 ,

我正在尝试使用图片中附加的公式验证帐号。

帐号将是字符串。最后,在正确执行上述验证后,我需要检查并返回正确的帐号。

帐号会有所不同,但在本例中,我们可以使用 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);
      }


}
4

2 回答 2

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;
      }
}
于 2017-02-15T07:49:04.093 回答
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;
    }

这是我最后尝试的。希望这可以帮助。

干杯!!!

于 2017-02-15T08:29:28.973 回答