【问题标题】:How would I add two int that are in the same array to each other and convert them into an int. In the Luhn Algorithm我如何将同一数组中的两个 int 添加到彼此并将它们转换为 int。在 Luhn 算法中
【发布时间】:2021-09-23 21:54:36
【问题描述】:

我正在尝试将数组的两个部分相加以形成一个 int 值。我正在使用 Luhn 算法来确定一张信用卡是一张有效的信用卡。我们只使用 6 位数的信用卡,以确保没有人输入真实的信用卡号码。我感到困惑的部分是当我去拆分一个大于 10 的数字并将它加在一起时。例如,如果算法要给我 12,我需要将它分成 1 和 2,然后将它们加在一起等于 3。我相信我目前在代码中将其拆分,但是当我将它们加在一起时,我得到了一些数字从那以后就没有了。这是一段代码,其中有一些注释。

我在某些地方打印了数字,以向自己展示某些地方正在发生的事情。我还添加了一些 cmets 说打印出来的数字是预期的,还有一些 cmets 用于当没有我预期的东西时

int[] cardNumber = new int[]{ 1,2,3,4,5,5};
    int doubleVariablesum = 0;
    int singleVariablesum = 0;
    int totalSum = 0;
    int cutOffVar = 0;
    String temp2;
    for (int i = cardNumber.length - 1; i >= 0;) { 
        int tempSum = 0;
        int temp = cardNumber[i];
        temp = temp * 2;
        System.out.println("This is the temp at temp * 2: " + temp);
        temp2 = Integer.toString(temp);
        if (temp2.length() == 1) { 
            System.out.println("Temp2 char 0: "+ temp2.charAt(0));
            // this prints out the correct number  
            // Example: if there number should be 4 it will print 4

            tempSum = temp2.charAt(0);  
            System.out.println("This is tempSum == 1: " + tempSum);
            // when this goes to add temp2.charAt(0) which should be 4 it prints out                      //something like 56

        } else {
            System.out.println("TEMP2 char 0 and char 1: " + temp2.charAt(0) + " " + temp2.charAt(1));

// 这会成功打印出正确的数字

            tempSum = temp2.charAt(0) + temp2.charAt(1);
            System.out.println("This is tempSum != 1: " + tempSum);
            // but here it when I try to add them together it is giving me something 
            // like 97 which doesn't make since for the numbers I am giving it
        }
        doubleVariablesum = tempSum + doubleVariablesum;
        System.out.println("This is the Double variable: " + doubleVariablesum);
        System.out.println();
        i = i - 2;
    }

【问题讨论】:

    标签: java arrays luhn


    【解决方案1】:

    因为您要将数字转换为字符串以拆分整数,然后尝试将它们重新加在一起。您实际上是将两个字符的数值相加,从而为您提供了奇数。您需要将其转换回整数,您可以使用 Integer.parseInt(String.valueOf(temp2.charAt(0)))

    【讨论】:

      【解决方案2】:

      当添加 char 符号 '0''1' 时,它们的 ASCII 值被添加 - 而不是数字 01

      将数字符号转换为int时,可以使用Character::getNumericValue方法或减去'0'

      但是,也可以计算 2 位数字中的数字总和,而无需像这样转换为 String 和 char 操作:

      int sum2digits = sum / 10 + sum % 10; // sum / 10 always returns 1 if sum is a total of 2 digits
      

      【讨论】:

        【解决方案3】:

        似乎 charAt() 类型转换为整数值,但 ascii 类型。因此对于字符“0”和“1”,将返回数字 48 和 49,从而得到总和 97。要解决此问题,您只需将 temp2 分配给 (temp / 10) + (temp % 10)。这实际上拆分了一个两位整数并将它们的总和相加。

        【讨论】:

          【解决方案4】:

          在处理char和String时需要注意以下几点

          1. charAt(index) 的结果分配给int 将分配ASCII 值,而不是实际的整数值。要获得实际值,您需要String.valueOf(temp2.charAt(0))

          2. 连接chars 的结果是ASCII 值的总和。 例如,如果char c = '1'; System.out.println(c + c); 将打印 "98" 而不是 "11"。 但是System.out.println("" + c + c); 将打印 "11"。注意 "" 将强制字符串连接。

          【讨论】:

            猜你喜欢
            • 2012-02-14
            • 2016-08-19
            • 1970-01-01
            • 1970-01-01
            • 2013-05-01
            • 2021-11-25
            • 2013-06-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多