在递归函数中使用Java递减运算符

时间:2022-04-18 18:05:07

Here is a recursive funtion -

这是一个递归函数 -

private void printVar01(int t){
    if(t != 0){
        logp.info("o: "+t);
        printVar01(t-1);
    }
}

Same funtion with a slight modification -

相同的功能略有修改 -

private void printVar02(int t){
    if(t != 0){
        logp.info("o: "+t);
        printVar02(t--);
    }
}

If I pass in a interger value like 4, printVar01 works as expected, where t decrements to 0 in successive recrsive calls, eventually causing the program to exit.

如果我传入一个像4这样的整数值,printVar01按预期工作,其中t在连续的recrsive调用中递减为0,最终导致程序退出。

With printVar02, t stays at value 4.

对于printVar02,t保持在值4。

Why? I am assuming this has something to do with variable assignment and/or how values are passed to funtions.

为什么?我假设这与变量赋值和/或值如何传递给函数有关。

3 个解决方案

#1


just use --t instead of t--

只需使用--t而不是t--

private static void printVar02(int t){
if(t != 0){
    logp.info("o: "+t);
    printVar02(--t);
}

#2


t-1 does not change t while t-- does.

当t--时t-1不改变t。

  • t-1 gives you new value without affecting the actual value of t
  • t-1为您提供新值而不影响t的实际值

  • t-- gives you t and than decreases the value of t
  • t--给你t而不是减去t的值

I think printVar02 should work fine and in printVar01 value remains same.

我认为printVar02应该可以正常工作,并且printVar01值保持不变。


For the comment of DoubleMa

对于DoubleMa的评论

actually 01 will works not 02, in 01 t is changing, but in 02 the function is calling itself before changing the value of T, he just need to use --t;

实际01将工作不02,在01 t正在改变,但在02中函数在改变T的值之前调用自己,他只需要使用--t;

I also definitely suspect the recursive call.

我也肯定怀疑递归电话。

If you mean printVar = printVar01 = printVar02.

如果你的意思是printVar = printVar01 = printVar02。

If you are calling printVar recursively than t-1 will work as recursive call will make it work and in t-- it will pass same value 4 every time as it's a postdecrement use predecrement --t instead.

如果你递归调用printVar而不是t-1将起作用,因为递归调用将使它工作,并且在t--它每次都会传递相同的值4,因为它是一个postdecrement使用predecrement --t而不是。

#3


Post decrement decrees value by 1 after execution of statement printVar(t--). Hence each time 4 is being passed to the function.
Use should use --t instead which does the decrement first.

在执行语句printVar(t--)之后,递减递减值减1。因此,每次将4传递给函数。使用应该使用--t而不是先减少。

#1


just use --t instead of t--

只需使用--t而不是t--

private static void printVar02(int t){
if(t != 0){
    logp.info("o: "+t);
    printVar02(--t);
}

#2


t-1 does not change t while t-- does.

当t--时t-1不改变t。

  • t-1 gives you new value without affecting the actual value of t
  • t-1为您提供新值而不影响t的实际值

  • t-- gives you t and than decreases the value of t
  • t--给你t而不是减去t的值

I think printVar02 should work fine and in printVar01 value remains same.

我认为printVar02应该可以正常工作,并且printVar01值保持不变。


For the comment of DoubleMa

对于DoubleMa的评论

actually 01 will works not 02, in 01 t is changing, but in 02 the function is calling itself before changing the value of T, he just need to use --t;

实际01将工作不02,在01 t正在改变,但在02中函数在改变T的值之前调用自己,他只需要使用--t;

I also definitely suspect the recursive call.

我也肯定怀疑递归电话。

If you mean printVar = printVar01 = printVar02.

如果你的意思是printVar = printVar01 = printVar02。

If you are calling printVar recursively than t-1 will work as recursive call will make it work and in t-- it will pass same value 4 every time as it's a postdecrement use predecrement --t instead.

如果你递归调用printVar而不是t-1将起作用,因为递归调用将使它工作,并且在t--它每次都会传递相同的值4,因为它是一个postdecrement使用predecrement --t而不是。

#3


Post decrement decrees value by 1 after execution of statement printVar(t--). Hence each time 4 is being passed to the function.
Use should use --t instead which does the decrement first.

在执行语句printVar(t--)之后,递减递减值减1。因此,每次将4传递给函数。使用应该使用--t而不是先减少。