zoukankan      html  css  js  c++  java
  • 递归,斐波那契,对象类型数组

    /*
    递归:
    方法自己调用自己
    
    递归有两个条件:
    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);
        }
    }
  • 相关阅读:
    oracle sql语句
    Block
    Bug调试
    Xcode 项目文件介绍
    Mac终端命令
    Objective-C命名编写规范
    2014-07-23 .NET实现微信公众号接入
    2014-07-22 如何成为一名合格的职业人士
    3、C# 文件处理工具集
    2、C# 编码/加密工具集
  • 原文地址:https://www.cnblogs.com/hbxZJ/p/15470131.html
Copyright © 2011-2022 走看看