【问题标题】:Omit Digit Method Bug省略数字方法错误
【发布时间】:2017-03-10 02:08:06
【问题描述】:

我在创建连续数字数组的方法时遇到问题(即,如果您输入 1 和 10 作为参数,该数组将包含 1-10 中的每个数字),然后将每个数字与另一个数字进行比较数字(例如 4) - 如果数字匹配(例如 4 == 4),则从数组中删除该数字。最后它返回那个数组。

我已经实现了下面的方法,它有时有效,但不是一直有效,我不知道为什么?

例如,如果我创建了一个新数组并打印了每个数组:

ArrayList<Integer> omittedDigitArray = new ArrayList<Integer>(Omit.allIntegersWithout(20, 45, 3));

        System.out.println("Array - Numbers with Omitted Digit:");
        for (int n : omittedDigitArray) {
            System.out.print(n + ", ");
        }

数组中省略了数字 29?谁能告诉我为什么?谢谢!

    // Creates the ArrayList
    ArrayList<Integer> numberList = new ArrayList<Integer>();

    // Loop creates an array of numbers starting at "from" ending at "to"
    for (int i = from; i < to + 1; i++) {
        numberList.add(i);
    }

    // Check the array to see whether number contains digit
    // Code checks whether x contains 5, n == one digit

    // IMPORTANT: Doesn't work on the first half of numbers i.e / will remove 3 but not 30
    for (int j = 0; j < numberList.size(); j++) {

        int number = (int) numberList.get(j);         // This can be any integer
        int thisNumber = number >= 0 ? number: -number;    // if statement in case argument is negative
        int thisDigit;

        while (thisNumber != 0) {

            thisDigit = thisNumber % 10;    // Always equal to the last digit of thisNumber
            thisNumber = thisNumber / 10;   // Always equal to thisNumber with the last digit chopped off, or 0 if thisNumber is less than 10

            if (thisDigit == omittedDigit) {
                numberList.remove(j);
                j--;
            }
        }
    }

    // Return the completed Array list
    return numberList;
}

}

【问题讨论】:

  • 当您从列表中删除项目时,您应该向后遍历列表。这样,您不必调整列表索引计数器。此外,您的说明没有说明数字是否在数字中。您的说明会说明数字是否匹配。

标签: java arrays while-loop


【解决方案1】:

您的内部循环有问题。从列表中删除一个元素后,您应该从该循环中中断。否则,您可能会删除不相关的附加数字(如果省略的数字在同一个数字中出现多次)。

    while (thisNumber != 0) {

        thisDigit = thisNumber % 10;    // Always equal to the last digit of thisNumber
        thisNumber = thisNumber / 10;   // Always equal to thisNumber with the last digit chopped off, or 0 if thisNumber is less than 10

        if (thisDigit == omittedDigit) {
            numberList.remove(j);
            j--;
            break; // add this
        }
    }

我运行了你的代码(+我建议的修复),范围为 1 到 50,省略了数字 4 并得到:

[1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 50]

在处理 44 时出现代码中的问题 - 删除它之后(由于前 4 个,您继续 while 循环,找到另一个 4 并删除另一个数字,恰好是 39,因为数字 40到 43 个已被删除)。

【讨论】:

    【解决方案2】:

    我建议的解决方案:

    ArrayList<Integer> numberList = new ArrayList<Integer>();
    
    // Loop creates an array of numbers starting at "from" ending at "to"
        for (int i = from; i < to + 1; i++) {
            numberList.add(i);
        }
    //removing all numbers that contain the digit
        numberList.removeIf(j->containsDigit(j,thisDigit));
        return numberList;
    }
    
    boolean containsDigit(int number,int thisDigit){
        //making sure thisDigit is positive
        thisDigit=Math.abs(thisDigit)
    
        //if thisDigit is not a digit result is false
        if(thisDigit>=10) return false;
    
        //breaking the number into its digits
        List<Integer> digits=new ArrayList<Integer>();
        while(number>0){
            digits.add(number%10);
            number=(int) number/10;
        }
    
        return digits.contains(thisDigit);
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2013-10-06
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多