我曾经在数学方面有过一种名为computewall的东西(在德语中:" Rechenwand",我不知道这是如何用英语调用的),你得到了金字塔和一些结果。它看起来像像这样:
0
0|0
0|0|0
4|0|0|0
5|9|0|0|0
因为我有假期,所以我想:"好吧,让我们编写一些代码"。所以我尝试编写一个程序来计算一个解决方案,其中数字1-15只在金字塔中出现一次。
计算两个数字之间一行数字的结果,就像您在左下角看到的那样。它是替换下面数字的绝对值。
我尝试过的项目在Github上,可以在这里找到:https://github.com/SchoolGuy/Computewall
问题是,每次运行程序时,l.57的do-while
循环都会导致类似无限循环的事情(实际上只是无穷无尽地试图拟合参数)。我的问题是如何摆脱这个问题!
P.S。:我注意到了很多要找到的确切点,所以不要害怕它的大小。
编辑02.04.2016 15:50:
package Main;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class computewall {
int[][] computewallInt = null;
int count;
ArrayList<Integer> sortedComputewall = null;
Random rnd = new Random();
public computewall() {
count = 0;
sortedComputewall = new ArrayList<>();
computewallInt = new int[5][5];
computewallInt[0][0] = 0;
computewallInt[1][0] = 0;
computewallInt[1][1] = 0;
computewallInt[2][0] = 0;
computewallInt[2][1] = 0;
computewallInt[2][2] = 0;
computewallInt[3][0] = 0;
computewallInt[3][1] = 0;
computewallInt[3][2] = 0;
computewallInt[3][3] = 0;
computewallInt[4][0] = 0;
computewallInt[4][1] = 0;
computewallInt[4][2] = 0;
computewallInt[4][3] = 0;
computewallInt[4][4] = 0;
calculate();
System.out.println("Rounds till solved: " + count);
System.out.println(" " + computewallInt[0][0]);
System.out.println(" " + computewallInt[1][0] + " | " + computewallInt[1][1]);
System.out.println(" " + computewallInt[2][0] + " | " + computewallInt[2][1] + " | " + computewallInt[2][2]);
System.out.println(" " + computewallInt[3][0] + " | " + computewallInt[3][1] + " | " + computewallInt[3][2] + " | " + computewallInt[3][3] + " | ");
System.out.println(computewallInt[4][0] + " | " + computewallInt[4][1] + " | " + computewallInt[4][2] + " | " + computewallInt[4][3] + " | " + computewallInt[4][4]);
}
public void calculate() {
boolean uniqueCheck = false;
ArrayList<String> usedPosibilities = new ArrayList<>();
System.out.println("-------------------------");
do {
count++;
sortedComputewall.clear();
boolean duplicateCheck = false;
boolean ohCheck = false;
//Set values and search for duplicates and 0
do {
computewallValueReplace(computewallInt, 4, 0, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 1, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 2, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 3, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 4, rnd.nextInt(16));
///*
if (computewallInt[4][0] == computewallInt[4][1] | computewallInt[4][0] == computewallInt[4][2] | computewallInt[4][0] == computewallInt[4][3] | computewallInt[4][0] == computewallInt[4][4]
| computewallInt[4][1] == computewallInt[4][2] | computewallInt[4][1] == computewallInt[4][3] | computewallInt[4][1] == computewallInt[4][4]
| computewallInt[4][2] == computewallInt[4][3] | computewallInt[4][2] == computewallInt[4][4] | computewallInt[4][3] == computewallInt[4][4]) {
duplicateCheck = true;
}
if (computewallInt[4][0] == 0 | (computewallInt[4][1] == 0) | (computewallInt[4][1] == 0) | (computewallInt[4][3] == 0) | computewallInt[4][4] == 0) {
ohCheck = true;
}
} while (duplicateCheck | ohCheck);
usedPosibilities.add(String.valueOf(computewallInt [4][0]) + String.valueOf(computewallInt [4][1]) + String.valueOf(computewallInt [4][2])
+ String.valueOf(computewallInt [4][3]) + String.valueOf(computewallInt [4][4]));
System.out.println("Lowest row calculated");
//Rest of calculating
computewallInt[3][0] = Math.abs(computewallInt[4][0] - computewallInt[4][1]);
computewallInt[3][1] = Math.abs(computewallInt[4][1] - computewallInt[4][2]);
computewallInt[3][2] = Math.abs(computewallInt[4][2] - computewallInt[4][3]);
computewallInt[3][3] = Math.abs(computewallInt[4][3] - computewallInt[4][4]);
computewallInt[2][0] = Math.abs(computewallInt[3][0] - computewallInt[3][1]);
computewallInt[2][1] = Math.abs(computewallInt[3][1] - computewallInt[3][2]);
computewallInt[2][2] = Math.abs(computewallInt[3][2] - computewallInt[3][3]);
computewallInt[1][0] = Math.abs(computewallInt[2][0] - computewallInt[2][1]);
computewallInt[1][1] = Math.abs(computewallInt[2][1] - computewallInt[2][2]);
computewallInt[0][0] = Math.abs(computewallInt[1][0] - computewallInt[1][1]);
System.out.println("Rest of table calculated");
sortedComputewall.add(computewallInt[0][0]);
sortedComputewall.add(computewallInt[1][0]);
sortedComputewall.add(computewallInt[1][1]);
sortedComputewall.add(computewallInt[2][0]);
sortedComputewall.add(computewallInt[2][1]);
sortedComputewall.add(computewallInt[2][2]);
sortedComputewall.add(computewallInt[3][0]);
sortedComputewall.add(computewallInt[3][1]);
sortedComputewall.add(computewallInt[3][2]);
sortedComputewall.add(computewallInt[3][3]);
sortedComputewall.add(computewallInt[3][3]);
sortedComputewall.add(computewallInt[4][4]);
sortedComputewall.add(computewallInt[4][1]);
sortedComputewall.add(computewallInt[4][2]);
sortedComputewall.add(computewallInt[4][3]);
Object[] sortedComputeWallInt = sortedComputewall.toArray();
Arrays.sort(sortedComputeWallInt);
if (sortedComputewall.contains(0) | !sortedComputewall.contains(1) | !sortedComputewall.contains(2) | !sortedComputewall.contains(3) | !sortedComputewall.contains(4) |
!sortedComputewall.contains(5) | !sortedComputewall.contains(6) | !sortedComputewall.contains(7) | !sortedComputewall.contains(8) | !sortedComputewall.contains(9) |
!sortedComputewall.contains(10) | !sortedComputewall.contains(11) | !sortedComputewall.contains(12) | !sortedComputewall.contains(13) | !sortedComputewall.contains(14) |
!sortedComputewall.contains(15)) {
uniqueCheck = true;
}
System.out.println("-------------------------");
} while (uniqueCheck);
}
public void computewallValueReplace(int[][] array, int row, int position, int value) {
array[row][position] = value;
}
}
这是完整的课程。
答案 0 :(得分:1)
此代码存在一些大问题,但我会回答您关于while循环的具体问题。这是你的代码:
do {
computewallValueReplace(computewallInt, 4, 0, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 1, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 2, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 3, rnd.nextInt(16));
computewallValueReplace(computewallInt, 4, 4, rnd.nextInt(16));
///*
if (computewallInt[4][0] == computewallInt[4][1] | computewallInt[4][0] == computewallInt[4][2] | computewallInt[4][0] == computewallInt[4][3] | computewallInt[4][0] == computewallInt[4][4]
| computewallInt[4][1] == computewallInt[4][2] | computewallInt[4][1] == computewallInt[4][3] | computewallInt[4][1] == computewallInt[4][4]
| computewallInt[4][2] == computewallInt[4][3] | computewallInt[4][2] == computewallInt[4][4] | computewallInt[4][3] == computewallInt[4][4]) {
duplicateCheck = true;
}
if (computewallInt[4][0] == 0 | (computewallInt[4][1] == 0) | (computewallInt[4][1] == 0) | (computewallInt[4][3] == 0) | computewallInt[4][4] == 0) {
ohCheck = true;
}
} while (duplicateCheck | ohCheck);
观察到,如果在此行为的第一次通过中将duplicateCheck
或ohCheck
设置为true
,那么您将陷入无限循环。它们以false
进入循环,但可以在循环的第一遍中设置为true
。循环中没有条件将其中任何一个设置为false,因此条件(duplicateCheck | ohCheck)
将始终满足。
答案 1 :(得分:0)
当其中一项检查(duplicateCheck | ohCheck)为真时,var从未设置为false,所以我有无限循环