• 4.递归


    递归的含义:递归,就是在运行的过程中调用自己。

          递归必须要有三个要素:

      ①、边界条件

      ②、递归前进段

      ③、递归返回段

      当边界条件不满足时,递归前进;当边界条件满足时,递归返回。

    1.求一个数的阶乘
    0!=1
    1!=1
    负数没有阶乘
    n! = n*(n-1)*(n-2)*......1

    /**
    * 0!=1 1!=1 * 负数没有阶乘,如果输入负数返回-1 * @param n * @return */ public static int getFactorial(int n){ if(n >= 0){
        //如果等于0直接返回1
    if(n==0){ System.out.println(n+"!=1"); return 1; }else{
           //如果不等于0,则自己调用自己 System.out.println(n);
    int temp = n*getFactorial(n-1); System.out.println(n+"!="+temp); return temp; } } return -1; }

    2.使用递归实现99乘法表
    public class DiGui {
    public static void chengfa(int i) {
    if (i == 1) {
    System.out.println("1*1=1");
    } else {
    chengfa(i - 1);
    for (int j = 1; j <= i; j++) {
    System.out.println(j + "*" + i + "=" + j * i + " ");
    }
    System.out.println(" ");
    }
    }

    public static void main(String[] args){
    chengfa(4);
    }
    }
  • 相关阅读:
    图片移动特效
    风云舞
    弹出页面
    javascript放大镜原版
    jquery UI入门
    AJAX demo——操作文本文件
    上一页 1 2 ...10 下一页 百度 GOOGLE 分页
    在后台调用JavaScript打开新页面
    Ext简介
    My97DatePicker
  • 原文地址:https://www.cnblogs.com/zhiaijingming/p/10507849.html
Copyright © 2020-2023  润新知