开发者

Why does this program loop infinitely?

I have the following code (it doesn't matter what it does, but for the curious, it's the start of an implementation of a square bending algorithm).

The problem is, it loops for no reason:

import java.util.*;
import java.io.*;
import java.lang.*;

class Solution
{
public static class kll
{
    int ni;
    int nj;
    int pi;
    int pj;
    int v;

    kll(){};
}

public static class g
{
    static kll[][] a;
    static int wh;
}

public static void f(int i1,int i2,int j1,int j2)
{
    int nj1,nj2;
    while (g.a[i1][i2].ni!=g.wh && g.a[i1][i2].nj!=g.wh)
    {
        i1=g.a[i1][i2].ni;
        i2=g.a[i1][i2].nj;
    }

    while (g.a[j1][j2].ni!=g.wh && g.a[j1][j2].nj!=g.wh)
    {
        j1=g.a[j1][j2].ni;
        j2=g.a[j1][j2].nj;
    }
    while (j1!=0)
    {
        nj2=g.a[j1][j2].pj;
        nj1=g.a[j1][j2].pi;
        g.a[i1][i2].ni=j1;
        g.a[i1][i2].nj=j2;
        g.a[j1][j2].pi=i1;
        g.a[j1][j2].pj=i2;
        i1=j1;
        i2=j2;
        j1=nj1;
        j2=nj2;
    }
    g.a[i1][i2].ni=g.wh;
    g.a[i1][i2].nj=g.wh;


}

public static void main(String[] args)
{
    try
    {
        FileWriter oos1 = new FileWriter("output.txt");
        File inTxt=new File("input.txt");
        Scanner kbd = new Scanner(inTxt);
        int input=kbd.nextInt();
        kbd.close();
        int number=(int)Math.pow(4,input);
        g.wh=(int)Math.sqrt(number);

        g.a=new kll[g.wh+1][g.wh+1];

        for(int i=0;i<g.wh+1;i++)
            for(int j=0;j<g.wh+1;j++)
                g.a[i][j]=new kll();

        for(int i=0;i<g.wh;i++)
            for(int j=0;j<g.wh;j++)
            {
                g.a[i][j].ni=g.wh;
                g.a[i][j].nj=g.wh;
                g.a[i][j].pi=0;
          开发者_运维百科      g.a[i][j].pj=0;
                g.a[i][j].v=0;
            }

        int separator=g.wh;
        int half;
        while(separator>1)
        {
            half=separator/2;
            for(int i=0;i<half;i++)
                for(int j=0;j<separator;j++)
                    f(j,i,j,separator-1-i);
            for(int i=0;i<half;i++)
                for(int j=0;j<separator;j++)
                    f(i,j,separator-1-i,j);
            separator=half;
        }

    }
    catch (FileNotFoundException ex) {}
    catch(IOException ex) {}
}

}

Where have I gone wrong? That is, where is the infinite loop? What is causing it, and how can I fix it?

EDIT:

I have tried the debugger and it showed me that the infinite loop is here:

while (j1!=0)
{
    nj2=g.a[j1][j2].pj;
    nj1=g.a[j1][j2].pi;
    g.a[i1][i2].ni=j1;
    g.a[i1][i2].nj=j2;
    g.a[j1][j2].pi=i1;
    g.a[j1][j2].pj=i2;
    i1=j1;
    i2=j2;
    j1=nj1;
    j2=nj2;
}

I don't have any idea why it's infinite. j1 has got to be zero when it takes nj1 which is also zero. What's going wrong?


Simply put, j1 is never set to 0, because nj1 is never set to 0, because g.a[ji][j2].pi is never set to 0.

Place breakpoints and inspect g.a[ji][j2].pi -- you will see that it is never 0, given a particular set of arguments.

In particular, you will want to look at the arguments that feed your f() function that are causing that function to infinitely loop.

Once you have determined the problematic arguments, you can place an if-statement that causes a breakpoint to be hit before that function is called -- if you have not already determined why f() fails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜