/* 递归: 方法自己调用自己 递归有两个条件: 1.有出口 2.不断向出口靠近 需求:1-100的和 */ public class DiGui { public static void main(String[] args) { int result=getSum(100); System.out.println("result = " + result); } public static int getSum(int num){ if(num==1){ return 1; } return num=num+getSum(num-1); } }
斐波那契
public class FeiBo { public static void main(String[] args) { int result=feibo(5); System.out.println("result = " + result); } public static int feibo(int n){ if (n==1 ||n==2){ return 1; } return feibo(n-1)+feibo(n-2); } }
对象类型数组
public class ArrObject { public static void main(String[] args) { Person p1=new Person(); p1.name="lin1"; p1.age=23; Person p2=new Person(); p2.name="lin2"; p2.age=22; Person arr1[]=new Person[]{p1,p2}; // for (int i = 0; i < arr1.length; i++) { // arr1[i].show(); // } for (Person p:arr1){ p.show(); } } } class Person{ String name; int age; public void show(){ System.out.println("name :"+name+" age :"+age); } }