Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在图形中打印路径有什么问题?_Java_While Loop_Do While - Fatal编程技术网

Java 在图形中打印路径有什么问题?

Java 在图形中打印路径有什么问题?,java,while-loop,do-while,Java,While Loop,Do While,我试图在迷宫中打印一条解决方案路径。每个单元格都有一个父单元格,从那里我们可以到达该单元格。我正在使用以下代码打印路径 do{ System.out.print("The parent of "+index1+","+index2+"="); System.out.println(theParents[index1][index2][0]+","+theParents[index1][index2][

我试图在迷宫中打印一条解决方案路径。每个单元格都有一个父单元格,从那里我们可以到达该单元格。我正在使用以下代码打印路径

                do{
                System.out.print("The parent of "+index1+","+index2+"=");
                System.out.println(theParents[index1][index2][0]+","+theParents[index1][index2][1]);
                index1=theParents[index1][index2][0];
                index2=theParents[index1][index2][1];
                }while(!(index1==2 && index2==1));
问题是跳过了一些单元格。我不知道为什么

7,7的父代=6,7

6,7的父代=5,7

5,7的父代=4,7

4,7的父代=3,7

3,7的父代=2,7

2,7=1,7//的父项,如您所见,我们现在应该打印1,7,但它跳过1,7并转到1,6

1,6的父项=1,5

1,5=2,5//的父项跳过2,5,直接转到2,3

2,4的父项=2,3

2,3的父项=1,3

1,2的父项=1,1

1,1的父项=0,1

0,0的父项=1,0

1,0的父项=2,0

代码有什么问题

index1=theParents[index1][index2][0];
index2=theParents[index1][index2][1]; // the index1 here is already reassigned!
在index2的分配中使用index1时,您已经重新分配了index1

你需要像这样的东西

int newindex1 = theParents[index1][index2][0];
int newindex2 = theParents[index1][index2][1];

index1 = newindex1;
index2 = newindex2;