Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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 递归函数StackOverflowerr_Java_Recursion - Fatal编程技术网

Java 递归函数StackOverflowerr

Java 递归函数StackOverflowerr,java,recursion,Java,Recursion,我正在编写僵尸感染城市居民的代码,而: 2:没有人 1:未受感染的人 0:僵尸 僵尸会感染僵尸周围的所有正常人。 下面是我的Java程序,但我得到了错误:StackOverflowerr public class InfectGame { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[][] = { { 1, 1,

我正在编写僵尸感染城市居民的代码,而: 2:没有人 1:未受感染的人 0:僵尸

僵尸会感染僵尸周围的所有正常人。 下面是我的Java程序,但我得到了错误:StackOverflowerr

public class InfectGame {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    int a[][] = { { 1, 1, 1, 1, 2, 2, 2, 1, 1, 0 },
            { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 1, 1, 1, 1, 0, 0, 1, 1, 1, 1 },
            { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
            { 1, 1, 1, 1, 0, 1, 1, 1, 2, 1 },
            { 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 2, 2, 2, 1, 1, 1, 1, 1, 1, 2 }, };

    int i = 0;
    int j = 0;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {

            if (a[i][j] == 0) {
                run_test(i, j, a, 0, 10);
            }
        }
    }

    i = 0;
    j = 0;
    for (i = 0; i < 10; i++) {
        System.out.print("\n");
        for (j = 0; j < 10; j++) {
            System.out.print(a[i][j] + " ");
        }
    }

}

public static void run_test(int x, int y, int a[][], int v, int size) {

    if ((x < 0) || (x >= size))
        return;
    if ((y < 0) || (y >= size))
        return;
    // System.out.print(a[x][y] + " ");
    // a[x][y] = v;
    if (a[x][y] != 2) {
        a[x][y] = v;
        if (x + 1 < size) {
            run_test(x + 1, y, a, v, size);
        }
        if (x > 0) {
            run_test(x - 1, y, a, v, size);
        }
        if (y + 1 < size) {
            run_test(x, y + 1, a, v, size);
        }
        if (y > 0) {
            run_test(x, y - 1, a, v, size);
        }
    }

}

}


    Exception in thread "main" java.lang.StackOverflowError
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
        at InfectGame.run_test(InfectGame.java:55)
        at InfectGame.run_test(InfectGame.java:58)
公共类游戏{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
int a[][]={{1,1,1,1,2,2,1,1,0},
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 2, 1, 1, 2, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 2, 1 },
{ 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 2, 2, 2, 1, 1, 1, 1, 1, 1, 2 }, };
int i=0;
int j=0;
对于(i=0;i<10;i++){
对于(j=0;j<10;j++){
如果(a[i][j]==0){
运行测试(i,j,a,0,10);
}
}
}
i=0;
j=0;
对于(i=0;i<10;i++){
系统输出打印(“\n”);
对于(j=0;j<10;j++){
系统输出打印(a[i][j]+“”);
}
}
}
公共静态无效运行测试(int x,int y,int a[][],int v,int size){
如果((x<0)| |(x>=大小))
返回;
如果((y<0)| |(y>=大小))
返回;
//系统输出打印(a[x][y]+“”);
//a[x][y]=v;
如果(a[x][y]!=2){
a[x][y]=v;
如果(x+1<尺寸){
运行测试(x+1,y,a,v,尺寸);
}
如果(x>0){
运行测试(x-1,y,a,v,尺寸);
}
如果(y+1<尺寸){
运行测试(x,y+1,a,v,尺寸);
}
如果(y>0){
运行测试(x,y-1,a,v,尺寸);
}
}
}
}
线程“main”java.lang.StackOverflowerr中出现异常
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
在InfectGame.run_测试(InfectGame.java:55)
在InfectGame.run_测试(InfectGame.java:58)
你被困在这里:

    if (x + 1 < size) {
        run_test(x + 1, y, a, v, size);
    }
    if (x > 0) {
        run_test(x - 1, y, a, v, size);
    }
if(x+10){
运行测试(x-1,y,a,v,尺寸);
}

因为x始终是第一个或第二个if-条件。

递归仅在具有终止条件时有效。 你没有。查看代码的这一部分:

    if (x + 1 < size) {
        run_test(x + 1, y, a, v, size);
    }
    if (x > 0) {
        run_test(x - 1, y, a, v, size);
    }
应该是:

if (a[x][y] != 2 && a[x][y] != v) {
否则,你会一次又一次地用僵尸填充相同的方块

但更多地考虑你的僵尸问题,它有点不同于普通的填充。在填充中,如果像素已经具有所需的颜色,则可以停止递归。 对于第一个僵尸来说,这是不正确的——从这个僵尸开始,它扩散开来。因此,第一次迭代不应该检查它是否已经是僵尸,但是递归的任何进一步迭代都应该检查,否则它不会终止

您可以这样做:

public static void run_test(int x, int y, int a[][], int v, int size, boolean first) {
    if ((x < 0) || (x >= size))
        return;
    if ((y < 0) || (y >= size))
        return;
    if (a[x][y] != 2 && (first || a[x][y] != v)) {
        a[x][y] = v;
        if (x + 1 < size) {
            run_test(x + 1, y, a, v, size, false);
        }
        if (x > 0) {
            run_test(x - 1, y, a, v, size, false);
        }
        if (y + 1 < size) {
            run_test(x, y + 1, a, v, size, false);
        }
        if (y > 0) {
            run_test(x, y - 1, a, v, size, false);
        }
    }
}

您应该使用更好的变量名。使用描述变量的内容,而不是x、y、z。

从何处获得此错误的详细信息?调用递归方法时,您不返回,这是您想要的吗?可能是重复的。你说得对。我在这里遇到了问题。你能给我一些建议来解决这个问题吗?我正在使用FillFlood算法来完成我的作业,但我不是很确定。谢谢你Erwin Bolwidt。我正在编写的程序是模拟感染每个僵尸的邻居的过程。我正在为我的程序应用的算法是递归洪水填充。(我的英语不是很好,所以如果有语法错误,请同情我)我在回答你的评论时添加了一些更多的信息。我已经根据你的代码完成了我的程序,我已经成功了!再次感谢,谢谢。我刚刚熟悉编程。你的建议将是我的经验。
public static void run_test(int x, int y, int a[][], int v, int size, boolean first) {
    if ((x < 0) || (x >= size))
        return;
    if ((y < 0) || (y >= size))
        return;
    if (a[x][y] != 2 && (first || a[x][y] != v)) {
        a[x][y] = v;
        if (x + 1 < size) {
            run_test(x + 1, y, a, v, size, false);
        }
        if (x > 0) {
            run_test(x - 1, y, a, v, size, false);
        }
        if (y + 1 < size) {
            run_test(x, y + 1, a, v, size, false);
        }
        if (y > 0) {
            run_test(x, y - 1, a, v, size, false);
        }
    }
}
    run_test(i, j, a, 0, 10, true);