如何在 Java 中只执行一次递归方法中的语句?

How to execute a statement within a recursive method only once in Java?

我只需要在Java中执行一次递归方法内的语句,不使用方法参数中的任何变量,方法外没有任何外部变量,并且语句必须在方法。

public static boolean recursiveMethod(int x) {
    if (x >= 5) {
        return true;
    }
    boolean isPrintedOnce = false;
    if (isPrintedOnce == false) {
        System.out.println("Printed once!"); // Print this statement only once
        isPrintedOnce = true;
    }
    return recursiveMethod(x + 1);
}

public static void main(String[] args) {
    System.out.println(recursiveMethod(0));
}

这行不通,因为"boolean isPrintedOnce = false;"执行了五次所以条件语句没用。我得到这个作为我的输出:

Printed once!
Printed once!
Printed once!
Printed once!
Printed once!
true

编辑:要求是, 1)不能在方法外使用外部变量 2) 在方法参数中不使用变量 3) 语句必须在方法内部 (我的作业需要这个。我可能看错了,因为这些要求似乎不可能,但我相信这些是其中的要求。)

带有第二个参数的解决方案将在每次外部方法调用时执行一次特殊代码。

public static boolean recursiveMethod(int x, boolean isFirstCall) {
    if (x >= 5) {
        return true;
    }

    if (isFirstCall) {
        System.out.println("Printed once!"); // Print this statement only once

    }
    return recursiveMethod(x + 1, false);
}

public static void main(String[] args) {
    System.out.println(recursiveMethod(0, true));
}

您可以通过重载该方法并制作 2 参数版本来隐藏第二个参数 private

public static boolean recursiveMethod(int x) {
    recursiveMethod(int x, true);
}

private static boolean recursiveMethod(int x, boolean isFirstCall) {
    if (x >= 5) {
        return true;
    }

    if (isFirstCall) {
        System.out.println("Printed once!"); // Print this statement only once

    }
    return recursiveMethod(x + 1, false);
}

public static void main(String[] args) {
    System.out.println(recursiveMethod(0);
}

用辅助方法包装它怎么样?

public static boolean recursiveMethodHelper(int x) {
    System.out.println("Printed once!");
    return recursiveMethod(x)
}

public static boolean recursiveMethod(int x) {
    if (x >= 5) {
        return true;
    }
    return recursiveMethod(x + 1);
}

public static void main(String[] args) {
    System.out.println(recursiveMethodHelper(0));
}

请注意,这具有始终打印的副作用,即使 x>=5 从一开始也是如此。但是外面没有声明变量,也没有加参数,按你的要求。

System.out.println("Printed once!"); 置于 if (x >= 5) 条件中,因为基本情况在递归调用中只应为真一次。

您可以使用所谓的辅助方法。这个想法是您将递归方法设为私有(实际上不需要私有)并接受它需要的所有内容作为参数。然后创建供开发人员使用的第二个方法,并通过传入一组特定参数来设置递归方法。

让我们看一个例子:

// This method is the one that our developer can use
public static boolean recursiveMethod(int x) {
    recursiveMethod(x, true); // Any time the recursive method is called from here, we know it's the first time!
}

// This is our real one, hidden from view and accessed only by our controlled helper
private static boolean recursiveMethod(int x, boolean firstTime) {
    if (x <= 5) {
        return true;
    }

    if (firstTime) {
        System.out.println("It's the first time!");
    }

    return recursiveMethod(x+1, false); // Anytime this method is called recursively here, we know it isn't the first time!
}  

这种辅助方法的好处是您不需要全球化任何东西,同时仍然对使用它的人隐藏不必要的属性。从his/her的角度来看,完全一样。

试试这个代码

public static boolean recursiveMethod(int x) {
if (x >= 5) {
    return true;
}
boolean isPrintedOnce = false;
if (isPrintedOnce = false) {
    if(x==0){
    System.out.println("Printed once!"); // Print this statement only once
    }
    isPrintedOnce = true;
}
return recursiveMethod(x + 1);
}

public static void main(String[] args) {
System.out.println(recursiveMethod(0));
}

你可以使用这个技巧从 Exception 对象中获取堆栈的深度,并且只在特定深度打印:

  public static boolean recursiveMethod(int x) {
    if (x >= 5) {
      return true;
    }
    Exception e = new Exception();
    e.fillInStackTrace();
    if (e.getStackTrace().length == 2) {
      System.out.println("Printed once!"); // Print this statement only once
    }
    return recursiveMethod(x + 1);
  }

  public static void main(String[] args) {
    System.out.println(recursiveMethod(0));
  }