Java递归后返回值(填桶工具)
Java returning value after recursion (bucket fill tool)
我制作了一个生成 tiles/pixels 的程序。
在这个程序中有一个用于填补空白和漏洞的功能,这是我打算如何工作的:
给一个方法一个整数限制。此方法然后通过类似油漆桶的递归,将其瓷砖重新绘制为新瓷砖(整数类型 3)并在遇到实心瓷砖时停止。
一旦该方法填充的图块超过限制,该方法就会退出,并且 returns 一个错误的陈述,表明该区域的面积大于给定的限制。否则,如果递归方法一起停止并填充了封闭区域内的所有空间而不违反此限制,则它 returns 为真。
这里的问题是我不知道如何让递归方法的程序"wait"在返回值之前停止工作。我试图在返回之前通过 while 循环创建一个 "waiting" 变量 运行,但结果不太好。
这里是源代码:
// Method called in other parts of the program.
private boolean checkNum(int x, int y, int limit){
int num = 0;
checkNum(x,y,num,limit,count); // I think the recursive methods following this one are carried out after the statement on the next line. That is the cause of the issue.
return tempBoolean;
}
//Recursive method called be previous one and itself.
private void checkNum(int x, int y, int num, int limit,int count){
tempBoolean = false;
if ((grid[x][y] == 3) || (grid[x][y] == 1)) {
return;
}
grid[x][y] = 3;
System.out.println(num);
num++;
if (num > limit) {
tempBoolean = false; // This system for setting an external variable and then returning it in a different method is probably not the right way to do it.
return;
}
checkNum(x + 1,y,num,limit,count);
checkNum(x - 1,y,num,limit,count);
checkNum(x,y + 1,num,limit,count);
checkNum(x,y - 1,num,limit,count);
}
您的代码存在一些问题,
- 我认为您根本不需要递归。一个循环就可以了。
tempBoolean
变量始终设置为 false;
- 您的
num
变量在每次递归调用后回退到原始值,因此递归将处理比 limit
. 更多的像素
- 如果您认为
return tempBoolean;
语句在递归调用完成之前执行,不,那不会发生。
要使用递归本身修复它,你可以这样,
// Method called in other parts of the program.
private boolean checkNum(int x, int y, int limit){
int num = 0;
num = checkNum(x,y,num,limit,count); // I think the recursive methods following this one are carried out after the statement on the next line. That is the cause of the issue.
return (num <= limit);
}
//Recursive method called be previous one and itself.
private int checkNum(int x, int y, int num, int limit,int count){
if (num > limit) {
return num;
}
if ((grid[x][y] == 3) || (grid[x][y] == 1)) {
return num;
}
grid[x][y] = 3;
System.out.println(num);
num++;
num = checkNum(x + 1,y,num,limit,count);
num = checkNum(x - 1,y,num,limit,count);
num = checkNum(x,y + 1,num,limit,count);
num = checkNum(x,y - 1,num,limit,count);
return num;
}
我制作了一个生成 tiles/pixels 的程序。 在这个程序中有一个用于填补空白和漏洞的功能,这是我打算如何工作的:
给一个方法一个整数限制。此方法然后通过类似油漆桶的递归,将其瓷砖重新绘制为新瓷砖(整数类型 3)并在遇到实心瓷砖时停止。
一旦该方法填充的图块超过限制,该方法就会退出,并且 returns 一个错误的陈述,表明该区域的面积大于给定的限制。否则,如果递归方法一起停止并填充了封闭区域内的所有空间而不违反此限制,则它 returns 为真。
这里的问题是我不知道如何让递归方法的程序"wait"在返回值之前停止工作。我试图在返回之前通过 while 循环创建一个 "waiting" 变量 运行,但结果不太好。
这里是源代码:
// Method called in other parts of the program.
private boolean checkNum(int x, int y, int limit){
int num = 0;
checkNum(x,y,num,limit,count); // I think the recursive methods following this one are carried out after the statement on the next line. That is the cause of the issue.
return tempBoolean;
}
//Recursive method called be previous one and itself.
private void checkNum(int x, int y, int num, int limit,int count){
tempBoolean = false;
if ((grid[x][y] == 3) || (grid[x][y] == 1)) {
return;
}
grid[x][y] = 3;
System.out.println(num);
num++;
if (num > limit) {
tempBoolean = false; // This system for setting an external variable and then returning it in a different method is probably not the right way to do it.
return;
}
checkNum(x + 1,y,num,limit,count);
checkNum(x - 1,y,num,limit,count);
checkNum(x,y + 1,num,limit,count);
checkNum(x,y - 1,num,limit,count);
}
您的代码存在一些问题,
- 我认为您根本不需要递归。一个循环就可以了。
tempBoolean
变量始终设置为false;
- 您的
num
变量在每次递归调用后回退到原始值,因此递归将处理比limit
. 更多的像素
- 如果您认为
return tempBoolean;
语句在递归调用完成之前执行,不,那不会发生。
要使用递归本身修复它,你可以这样,
// Method called in other parts of the program.
private boolean checkNum(int x, int y, int limit){
int num = 0;
num = checkNum(x,y,num,limit,count); // I think the recursive methods following this one are carried out after the statement on the next line. That is the cause of the issue.
return (num <= limit);
}
//Recursive method called be previous one and itself.
private int checkNum(int x, int y, int num, int limit,int count){
if (num > limit) {
return num;
}
if ((grid[x][y] == 3) || (grid[x][y] == 1)) {
return num;
}
grid[x][y] = 3;
System.out.println(num);
num++;
num = checkNum(x + 1,y,num,limit,count);
num = checkNum(x - 1,y,num,limit,count);
num = checkNum(x,y + 1,num,limit,count);
num = checkNum(x,y - 1,num,limit,count);
return num;
}