Java递归--实现1+2+3+4+5
1 //方法递归(Recursion)1-->2-->3-->4-->3-->2-->1 2 //求1+……+4的和 3 public class RecursionTest01 4 { 5 public static void main(String[] args) 6 { 7 System.out.println("Hello World!"); 8 System.out.println(sumInt(4)); 9 } 10 public static int sumInt(int n){ 11 if(n==1){ 12 return 1;//为什么要写if语句,不写if语句sumInt(1-1)就会报错; 13 } 14 return n+sumInt(n-1); 15 } 16 }
花香蜜自来;书山有路勤为径,学海无涯苦作舟;常回忆看看。

浙公网安备 33010602011771号