具有最小/最大数字的数组索引的顺序倒计时

时间:2017-05-20 19:50:13

标签: java arrays

我正在尝试让我的应用程序获取一组随机数,并使用rnd(1..8)的所有可能组合。组合是索引数组(长度在2-99之间)。我已经模仿了一些测试代码来展示我到目前为止所拥有的内容,并且包含了我在底部寻找的结果。

为了成功做到这一点,我们想看看功能......

private static int[] updateArray(int[] array){
    for (int index = array.length - 1 ; index >= 0; index--){
        if (array[index] > 1){
            array[index]--;
            return array;
        } else if (array[index] == 1 && index != 0) {
            if (array[index - 1] != 1) {
                array[index - 1]--;
                array[index] = maxNum;
                return array;
            }
        }
        index--;
    }

    return array;
}

注意:我可能没有使用上面的正确算法。

以下是编译结果的完整代码......

public class Test {
    private static final int maxNum = 8;

    private static int[] updateArray(int[] array){
        for (int index = array.length - 1 ; index >= 0; index--){
            if (array[index] > 1){
                array[index]--;
                return array;
            } else if (array[index] == 1 && index != 0) {
                if (array[index - 1] != 1) {
                    array[index - 1]--;
                    array[index] = maxNum;
                    return array;
                }
            }
            index--;
        }

        return array;
    }

    private static boolean isArrayIndexesAllOnes(int[] array){
        for (int element: array){
            if (element != 1){
                return false;
            }
        }
        return true;
    }

    private static void printArrayCountdown(int[] array){
        boolean downToOne = false;

        while (!downToOne){
            array = updateArray(array);
            printArray(array);
            if (isArrayIndexesAllOnes(array)){
                downToOne = true;
            }
        }
    }

    private static void printArray(int[] array){
        for (int index = 0; index < array.length; index++){

            if (index == 0) {
                System.out.print("{" + array[index] + ",");
            } else if (index > 0 && index < array.length - 1){
                System.out.print(array[index] + ",");
            } else {
                System.out.println(array[index] + "}");
            }
        }
    }

    public static void main(String[] args) {
        int[] firstArray = {maxNum,maxNum};
        int[] secondArray = {maxNum,maxNum,maxNum};

        printArrayCountdown(firstArray);
        System.out.println();
        System.out.println();
        printArrayCountdown(secondArray);
    }
}

firstArray ...

的预期输出
{8,7}
{8,6}
{8,5}
{8,4}
{8,3}
{8,2}
{8,1}
{7,8}
{7,7}
{7,6}
{7,5}
{7,4}
{7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)

1 个答案:

答案 0 :(得分:1)

我要感谢anishthecoder的响应,真正帮助我缩小范围。基本上,我只需删除index--(如他所说),并添加一个额外的for循环。请参阅下面的代码......

private static int[] updateArray(int[] array){
    for (int index = array.length - 1 ; index >= 0; index--){
        if (array[index] > 1){
            array[index]--;
            return array;
        } else if (array[index] == 1 && index != 0) {
            if (array[index - 1] != 1) {
                array[index - 1]--;
                for (int i = index; i < array.length; i++){
                    array[i] = maxNum;
                }
                return array;
            }
        }
    }

    return array;
}

我确信有更简洁的方法可以做到这一点,我很乐意将答案提供给能够提出更清洁解决方案的任何人。现在这个工作......

再次感谢anishthecoder:)