我试图打印数字从1到10而不使用java中的循环.当n + 1传递给第6行的recursivefun方法调用时,它工作正常.但是当传递n ++时,代码会抛出一个错误:/
public class PrintWithoutUsingLoops {
public static void recursivefun(int n) {
if (n <= 10) {
System.out.println(n);
recursivefun(n++);//an exception is thrown at this line.
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
recursivefun(1);
}
}
Run Code Online (Sandbox Code Playgroud)