我想询问是否有办法缩短以下代码。 我认为应该有一种方法来进行递归但我不确定它因为recursiv不是我最强的一面:/ 稍后在每个计算状态下应该发生一些事情,但这只是一个怪物:/
public void VertexState() { // Try to find the next vertex state to test
int end = 0;
int counter = 0;
int place;
int up = 1;
int zaehler = 1;
try {
for (int u = 0; u < 4; u++) {
for (int t = 0; t < 4; t++) {
for (int s = 0; s < 4; s++) {
for (int r = 0; r < 4; r++) {
for (int q = 0; q < 4; q++) {
for (int p = 0; p < 4; p++) {
for (int o = 0; o < 4; o++) {
for (int n = 0; n < 4; n++) {
for (int m = 0; m < 4; m++) {
for (int l = 0; l < 4; l++) {
for (int k = 0; k < 4; k++) {
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
for (int h = 0; h < 4; h++) {
for (int g = 0; g < 4; g++) {
for (int f = 0; f < 4; f++) {
for (int e = 0; e < 4; e++) {
for (int d = 0; d < 4; d++) {
for (int c = 0; c < 4; c++) {
for (int b = 0; b < 4; b++) {
for (int a = 0; a < 4; a++) {
work[0][findPlace()].countUp();
work[findPlace()][0].countUp();
}
work[0][findNextPlace(1)].countUp();
work[findNextPlace(1)][0].countUp();
}
work[0][findNextPlace(2)].countUp();
work[findNextPlace(2)][0].countUp();
}
work[0][findNextPlace(3)].countUp();
work[findNextPlace(3)][0].countUp();
}
work[0][findNextPlace(4)].countUp();
work[findNextPlace(4)][0].countUp();
}
work[0][findNextPlace(5)].countUp();
work[findNextPlace(5)][0].countUp();
}
work[0][findNextPlace(6)].countUp();
work[findNextPlace(6)][0].countUp();
}
work[0][findNextPlace(7)].countUp();
work[findNextPlace(7)][0].countUp();
}
work[0][findNextPlace(8)].countUp();
work[findNextPlace(8)][0].countUp();
}
work[0][findNextPlace(9)].countUp();
work[findNextPlace(9)][0].countUp();
}
work[0][findNextPlace(10)].countUp();
work[findNextPlace(10)][0].countUp();
}
work[0][findNextPlace(11)].countUp();
work[findNextPlace(11)][0].countUp();
}
work[0][findNextPlace(12)].countUp();
work[findNextPlace(12)][0].countUp();
}
work[0][findNextPlace(13)].countUp();
work[findNextPlace(13)][0].countUp();
}
work[0][findNextPlace(14)].countUp();
work[findNextPlace(14)][0].countUp();
}
work[0][findNextPlace(15)].countUp();
work[findNextPlace(15)][0].countUp();
work[0][findNextPlace(16)].countUp();
work[findNextPlace(16)][0].countUp();
}
work[0][findNextPlace(17)].countUp();
work[findNextPlace(17)][0].countUp();
}
work[0][findNextPlace(18)].countUp();
work[findNextPlace(18)][0].countUp();
}
work[0][findNextPlace(19)].countUp();
work[findNextPlace(19)][0].countUp();
}
work[0][findNextPlace(20)].countUp();
work[findNextPlace(20)][0].countUp();
}
work[0][findNextPlace(21)].countUp();
work[findNextPlace(21)][0].countUp();
}
} catch (NullPointerException e) {
System.out.println("over and out");
}
}
public int findPlace() {
int place = work.length;
for (place = work.length - 1; place >= 0; place--) {
if (work[0][place] != null) {
return place;
}
}
return 0;
}
我真的希望你能帮助我:) 非常感谢你
答案 0 :(得分:0)
我不会问为什么你需要这个,但我认为这将是递归形式:
void recursiveImpl(int iterator)
{
iterator++;
if (iterator == 1)
{
for (int a = 0; a < 4; a++)
{
recursiveImpl(iterator);
work[0][findPlace()].countUp();
work[findPlace()][0].countUp();
}
}
else
{
for (int a = 0; a < 4; a++)
{
if (iterator < 22)
{
recursiveImpl(iterator);
}
work[0][findNextPlace(iterator)].countUp();
work[findNextPlace(iterator)][0].countUp();
}
}
}
答案 1 :(得分:0)
我看到了模式。
0
迭代到3
something
work[0][findNextPlace(21)].countUp();
和work[findNextPlace(21)][0].countUp();
something
只是你的模式,但传递给findNextPlace
的号码不同。
在将0
传递给findNextPlace
之前,请执行此操作。
让我们重写你的代码:
void something(int parameter) {
if(parameter < 0) {
for(int i = 0; i < 4; i++) {
work[0][findPlace(parameter)].countUp();
work[findPlace(parameter)][0].countUp();
}
} else {
for(int i = 0; i < 4; i++) {
something(parameter - 1);
work[0][findNextPlace(parameter)].countUp();
work[findNextPlace(parameter)][0].countUp();
}
}
}
然后,您只需用for
;
something(21)
金字塔