I'm trying to get my application to get an array of random numbers, and use all possible combinations of rnd(1..8). The combinations are an array of indexes (length between 2-99). I've mimicked some test-code to show what I have so far, and included the results I'm looking for at the bottom.
我正在尝试让我的应用程序获得一组随机数,并使用rnd(1..8)的所有可能组合。组合是索引数组(长度在2-99之间)。我已经模仿了一些测试代码来展示我到目前为止所拥有的内容,并且包含了我在底部寻找的结果。
To do this successfully, we want to look at function...
为了成功做到这一点,我们想看看功能......
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;
}
Note: I may not be using the correct algorithm above.
注意:我可能没有使用上面的正确算法。
Here is the full code to compile results...
这是编译结果的完整代码......
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);
}
}
Expected Output of the firstArray
...
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,3}
{7,2}
{7,1}
{6,8}
{6,7}
{6,6}
{6,5}
{6,4}
{6,3}
{6,2}
{6,1}
{5,8}
{5,7}
{5,6}
{5,5}
{5,4}
{5,3}
{5,2}
{5,1}
{4,8}
{4,7}
{4,6}
{4,5}
{4,4}
{4,3}
{4,2}
{4,1}
{3,8}
{3,7}
{3,6}
{3,5}
{3,4}
{3,3}
{3,2}
{3,1}
{2,8}
{2,7}
{2,6}
{2,5}
{2,4}
{2,3}
{2,2}
{2,1}
{1,8}
{1,7}
{1,6}
{1,5}
{1,4}
{1,3}
{1,2}
{1,1}
Success!
But with an array of more than 2 indexes (e.g.: secondArray
), I get this output:
但是对于一个包含2个以上索引的数组(例如:secondArray),我得到了这个输出:
{8,8,7}
{8,8,6}
{8,8,5}
{8,8,4}
{8,8,3}
{8,8,2}
{8,8,1}
{8,7,8}
{8,7,7}
{8,7,6}
{8,7,5}
{8,7,4}
{8,7,3}
{8,7,2}
{8,7,1}
{8,6,8}
{8,6,7}
{8,6,6}
{8,6,5}
{8,6,4}
{8,6,3}
{8,6,2}
{8,6,1}
{8,5,8}
{8,5,7}
{8,5,6}
{8,5,5}
{8,5,4}
{8,5,3}
{8,5,2}
{8,5,1}
{8,4,8}
{8,4,7}
{8,4,6}
{8,4,5}
{8,4,4}
{8,4,3}
{8,4,2}
{8,4,1}
{8,3,8}
{8,3,7}
{8,3,6}
{8,3,5}
{8,3,4}
{8,3,3}
{8,3,2}
{8,3,1}
{8,2,8}
{8,2,7}
{8,2,6}
{8,2,5}
{8,2,4}
{8,2,3}
{8,2,2}
{8,2,1}
{8,1,8}
{8,1,7}
{8,1,6}
{8,1,5}
{8,1,4}
{8,1,3}
{8,1,2}
{8,1,1}
{7,1,1}
{6,1,1}
{5,1,1}
{4,1,1}
{3,1,1}
{2,1,1}
{1,1,1}
Logic Error!
Instead, here is the expected output...
相反,这是预期的输出......
{8,8,7}
{8,8,6}
{8,8,5}
{8,8,4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{1,1,1}
Any ideas how to get this kind of behavior?
任何想法如何获得这种行为?
Note: I don't want to actually print out {ect...}
, for the purpose of not adding 512 lines on this question, I just cut out about 400 lines.
注意:我不想实际打印{ect ...},为了不在这个问题上添加512行,我只剪出了大约400行。
Thanks :)
1 个解决方案
#1
1
I'd like to thank anishthecoder response for really helping me narrow it down. Basically, I just had to remove the index--
(as he said), and add an additional for loop. See code below...
我要感谢anishthecoder响应,真正帮助我缩小范围。基本上,我只需要删除索引 - (如他所说),并添加一个额外的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;
}
I'm sure there is a cleaner way to do this, and I would be happy to give the answer points to anyone who can come up with a cleaner solution. For now this is working...
我确信有一种更清洁的方法可以做到这一点,我很乐意给那些能够提出更清洁解决方案的人提供答案。现在这个工作......
Thanks again anishthecoder :)
再次感谢anishthecoder :)
#1
1
I'd like to thank anishthecoder response for really helping me narrow it down. Basically, I just had to remove the index--
(as he said), and add an additional for loop. See code below...
我要感谢anishthecoder响应,真正帮助我缩小范围。基本上,我只需要删除索引 - (如他所说),并添加一个额外的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;
}
I'm sure there is a cleaner way to do this, and I would be happy to give the answer points to anyone who can come up with a cleaner solution. For now this is working...
我确信有一种更清洁的方法可以做到这一点,我很乐意给那些能够提出更清洁解决方案的人提供答案。现在这个工作......
Thanks again anishthecoder :)
再次感谢anishthecoder :)