zoukankan      html  css  js  c++  java
  • java 04 函数和递归

    class functiondemo{
    public static void main(String[] args){
    //调用函数
    int red = add(1,2);
    System.out.println(red);

    printemtytriangle(5);
    printemtytriangle(7);

    int mx2 = getmax(1,2,3);
    System.out.println(mx2);
    __________________________________
    //函数套用
    int max3 =getmax1(4,5);
    max3 =getmax1(max3,8);// getmax1(getmax1(4,5),5); 函数调用函数
    System.out.println(max3); //System.out.println(getmax1(getmax1(4,5),5));
    System.out.println(fabric(5));

    hello();


    }
    _____________________________________
    //定义函数add,两个数相加
    public static int add( int a ,int b){
    int c =a+b;
    return c;
    }


    ____________________________________________________
    //定义了printemtytriangle打印空心三角形的函数
    public static void printemtytriangle (int line){
    for (int i =0 ; i<line;i++){
    for(int j =0; j<line-1-i;j++){
    System.out.print(" ");
    }
    System.out.print("*");
    if (i ==(line -1)){
    for(int j = 0; j<2*i;j++){
    System.out.print("*");
    }
    }
    if ( i !=0 && i!=(line-1)) {
    for(int j =0; j<2*i -1;j++){
    System.out.print(" ");
    }
    System.out.print("*");
    }
    System.out.print(" ");
    }
    }

    _____________________________________________________
    public static int getmax(int a1,int b1,int c1){
    return (a1 > b1 ? (a1>c1 ? a1:c1) : (b1>c1 ? b1:c1));
    }


    ____________________________________________________________
    //四个数比较,函数套用
    public static int getmax1(int a2,int b2){
    return (a2 > b2? a2:b2);
    }

    ______________________________________________
    //阶乘
    public static int fabric(int n){
    int sum = 1;
    for (int i = 1 ; i<=n ; i++){
    sum =sum * i;
    }
    return sum;
    }

    __________________________________________________
    //递归需要终止条件
    public static void hello(){
    System.out.print("hello how are you");
    //hello();//自己调用自己跟死循环的区别在于会出现异常
    }

    _________________________________________________
    //阶乘的第二种方法,但是需要终止条件
    //n*(n-1)*(n-1-1)....
    public static int fabric2(int n){
    if (n ==1){
    return1;
    }
    return n*fabric(n-1);
    }


    }

  • 相关阅读:
    在通达信里制作自己的指数
    Android Studio实现代码混淆
    python 安装anaconda, numpy, pandas, matplotlib 等
    阿里云服务(一) OSS
    阿里云存储OSS之九大使用技巧
    用云存储和CDN轻松搞定网站图片
    阿里云开放服务oss的api
    'htmlentities(): charset `utf8' not supported, assuming utf-8'
    TP自动生成模块目录
    TP框架中APP_SUB_DOMAIN_DEPLOY什么意思?
  • 原文地址:https://www.cnblogs.com/simly/p/10007193.html
Copyright © 2011-2022 走看看