简体   繁体   中英

Why is my recursive loop printing the same value twice? - JAVA

The following code recursively reduces x by 1 until it is equal to y. It prints a string representing the recursion process. I do not want the starting value of x to be printed in the string.

private static String createPath(int x, int y, String path) {
        if (x > y) {
            path += "(" + (x-1) + "," + y + ") ";
            return createPath(x - 1, y, path);
        }
        else 
            return path += "(" + x + "," + y + ") ";
    }

When I input something such as:

System.out.println(createPath(5, 1, ""));

I get the following output:

(4,1) (3,1) (2,1) (1,1) (1,1) 

Why is the final value (1,1) being printed twice? I noticed that when I remove (x-1) from

path += "(" + (x-1) + "," + y + ") ";

The output becomes:

(5,1) (4,1) (3,1) (2,1) (1,1)

But once again, I do not want (5,1) to be printed in the string. Also, I have had a look at Why is my recursive loop printing the last value twice at the end? but it did not seem to help me.

仅仅因为当x==2y==1条件x>y为真并且"(" + (x-1) + "," + y + ") "返回 (1,1)

Your definition should be as follows:

private static String createPath(int x, int y, String path) {
    if (x <= y) {
        return path;
    }
    path += "(" + (x - 1) + "," + y + ") ";
    return createPath(x - 1, y, path);
}

This definition will ensure that as soon as x <= y , the value of path will be returned.

Correct solution:

    private static String createPath(int x, int y, String path) {
        if (x > (y + 1))
            return createPath(x - 1, y, path + "(" + (x - 1) + "," + y + ") ");
        else 
            return path + "(" + (x - 1) + "," + y + ") ";
    }

note, if (x > (y + 1)) and (x - 1) in else branch.

the execution order of your function is the following:

createPath(5, 1) => createPath(4, 1, "(4,1)");
createPath(4, 1) => createPath(3, 1, "(3,1)");
createPath(3, 1) => createPath(2, 1, "(2,1)");
createPath(2, 1) => createPath(1, 1, "(1,1)");
// and the last one in the else branch
createPath(1, 1) => createPath(1, 1, "(1,1)");

as you can see you need to add (x - 1) in the else branch and modify your if statement in such way that you get to the else branch earlier to prevent it from going to below 1.

Turns out that the reason why the final value was being printed twice was because the else return statement was adding the final value of (x,y) unnecessarily. For example,

else 
      return path += "(" + x + "," + y + ") ";

The above code is executed once x is no longer greater than y , meaning the value of path that was passed through createPath(int x, int y, String path) contains (x,y) which are equal to one another. However, what the else statement above does is once again add the values of x and y to path , when all that needs to be done is return path; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM