1 public static void main(String[] args){ 2 int N=100; 3 PrinterN(N); 4 } 5 6 private static void PrinterN(int n) { 7 for(int i = 0;i<=n;i++){ 8 System.out.println(i); 9 } 10 }
1 public static void main(String[] args){ 2 int N=100; 3 PrinterN(N); 4 } 5 6 private static void PrinterN(int n) { 7 if(n>=0){ 8 PrinterN(n-1); 9 System.out.println(n); 10 } 11 }
但是用递归实现时当n超过100000就会报错了java.lang.StackOverflowError