使用递归如何保持局部变量更新

发布于 2024-12-13 09:17:38 字数 373 浏览 1 评论 0原文

使用递归如何保持局部变量更新直到满足条件。我下面有一个例子很好地解释了为什么要问这个问题。计数变量是一个局部变量,每次方法执行时计数都会设置为 0。我无法将计数移到方法之外,它必须是局部变量而不是静态变量或其他任何变量。所需的输出应该是 (3 6 )

public static int returnInt(int b) {

        int count = 0;

        if (b == 6) {

            System.out.println(count + " " + b);
        }

        b += 2;
        count++;

        return returnInt(b);

    }

Using recursion how can i keep the local variable updated till a condition is met. I have an instance below that explains why question very well. The count variable is a local variable and very time the method goes through the count is set to 0. I cannot move the count outside the method it has to be local variable not static or anything else.. The desired output should be (3 6)

public static int returnInt(int b) {

        int count = 0;

        if (b == 6) {

            System.out.println(count + " " + b);
        }

        b += 2;
        count++;

        return returnInt(b);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月打碎记忆 2024-12-20 09:17:38

count 作为附加参数传递给该方法:

public static int returnInt(int b, int count) {

    ... do some stuff to b, print something, check whether end-condition has been met ...

    return returnInt(b, count + 1);
}

然后,使用额外参数 0 调用 returnInt 以开始操作。

Pass count as an additional parameter to the method:

public static int returnInt(int b, int count) {

    ... do some stuff to b, print something, check whether end-condition has been met ...

    return returnInt(b, count + 1);
}

Then, call returnInt with an extra argument of 0 to start things off.

撩起发的微风 2024-12-20 09:17:38

你不能。局部变量根据定义是局部的。它仅存在于该方法的当前执行范围内。

但你有以下解决方案。

最好的方法是将其作为方法的参数传递(与其他参数 b 完全相同)。

public static int returnInt(int b) {
    return returnInt(b, 0)
}
private static int returnInt(int b, int count) {
    // your code here. 
}

如果(由于某种奇怪的原因)您无法更改方法签名,您可以将此变量放入 <在这种情况下,即使多个线程同时执行您的方法,您的代码仍保持线程安全,

这是最糟糕的解决方案,因为它不是线程安全的并且破坏了封装。

You cannot. Local variable is local by definition. It exists only in scope of current execution of the method.

BUT you have the following solutions.

The best one is to pass it as a parameter of your method (exactly as you do with other parameter b.

public static int returnInt(int b) {
    return returnInt(b, 0)
}
private static int returnInt(int b, int count) {
    // your code here. 
}

If (for some strange reason) you cannot change the method signature you can put this variable into ThreadLocal. In this case even if several threads execute your method simultaneously your code stays thread safe.

Moving this variable to class level is the worst solution cause it is not thread safe and breaks encapsulation.

花之痕靓丽 2024-12-20 09:17:38

这个函数会溢出你的堆栈。使用递归时必须提供出路。至于变量count,你每次都将它重新定义回0;它不能在递归函数内初始化。尝试将它作为参数传递给您的函数,并为您的函数提供一条出路。

public static int returnInt(int b,int count) {

    if (b == 6) {

        System.out.println(count + " " + b);
        return b;
    }
    b+=2;
    count++;
    return returnInt(b);
}

“return b”行可以是您的出路...并且您不一定必须 return b...返回您需要的任何内容。

This function will overflow your stack. You must provide a way out when using recursion. And as for the variable count, you are redefining it back to 0 every time; it cannot be initialized inside the recursive function. Try passing it to your function as a parameter, and also providing a way out for your function.

public static int returnInt(int b,int count) {

    if (b == 6) {

        System.out.println(count + " " + b);
        return b;
    }
    b+=2;
    count++;
    return returnInt(b);
}

the 'return b' line can be your way out...and you don'y necessarily have to return b... return whatever you need.

不再见 2024-12-20 09:17:38
public static int returnInt(int b) {
    return returnInt(b, 0);
}

private static int returnInt(int b, int count) {    
    if (b == 6) {
        System.out.println(count + " " + b);
        // My guess is you should be returning something here???

        // Actually, this shouldn't be your exit point from the recursion because depending on the starting value of b, since you are always adding 2, it might never equal 6, so again you would get a StackoverflowException.
    }    
    b += 2;
    count++;

    return returnInt(b, count);
}
public static int returnInt(int b) {
    return returnInt(b, 0);
}

private static int returnInt(int b, int count) {    
    if (b == 6) {
        System.out.println(count + " " + b);
        // My guess is you should be returning something here???

        // Actually, this shouldn't be your exit point from the recursion because depending on the starting value of b, since you are always adding 2, it might never equal 6, so again you would get a StackoverflowException.
    }    
    b += 2;
    count++;

    return returnInt(b, count);
}
夜雨飘雪 2024-12-20 09:17:38

一般来说,创建 returnIntInner(int b, 装箱计数),将大部分逻辑移至该位置,然后从 returnInt 的“精简”版本调用它。装箱 count 的“旧式”方式是将计数作为 int[1] 数组传递,以便可以返回它——我还没有研究新的 Java 参考 parm 语法。但由于您的代码是尾递归的,并且不需要在递归调用后访问 count,因此您只需将 count 作为常规参数传递即可。

In general, create returnIntInner(int b, boxed count), move the bulk of the logic to that, and call that from a "thin" version of returnInt. To box count the "old style" way is to pass the count as an int[1] array, so that it can be returned -- I've not studied up on the new Java reference parm syntax. But since your code is tail-recursive and doesn't need to access count after the recursive call, you can simply pass count as a regular parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文