无聊的冷知识,Java小小小练习,你学废了 码

在这里插入图片描述
需要图图的私信我❀❀❀❀❀❀❀

1、break和continue用法介绍

如果在循环中 遇到了break 代表整个循环要结束了
而且break必须放到循环里面 除了swith
例如:

    public static void main(String[] args) {
        int i = 0;
        while (i<=10){
            if (i==3){
                break;
            }
            System.out.println(i);
            i++;
        }
    }
    运行代码结果为:
    0
    1
    2

    Process finished with exit code 0

与直对应的就是continue了

    public static void main(String[] args) {
        int i = 0;
        while (i<=10){
            if (i==3){
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
    运行代码结果为:
    0
    1
    2
    ...........................

将break换为continue
在这里的作用是返回循环起始点 这里的运行结果是0 1 2死循环.
遇到continue 它下方的代码将不会执行

在循环中continue前面加入 i++

    public static void main(String[] args) {
        int i = 0;
        while (i<=10){
            if (i==3){
                i++;
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
    运行结果:
    0
    1
    2
    4
    5
    6
    7
    8
    9
    10

    Process finished with exit code 0

小练习,巩固一下
找到1 - 100 之间既能被3整除 又能被5整除的数字

    public static void main(String[] args) {
        for (int i = 1; i < 101; ) {
            if (i%15 != 0){
                i++;
                continue;
            }
            System.out.print(i+" ");
            i++;
        }
        System.out.println();
    }
    运行结果:
    15 30 45 60 75 90 

    Process finished with exit code 0

也可以这样做: 效果相同

    public static void main(String[] args) {
        int i =1;
        while (i <= 100){
            if (i % 15 != 0){
                i++;
                continue;
            }
            System.out.print(i+" ");
            i++;
        }
        System.out.println();
    }

2、登陆问题

关于密码的登录:

    public static void login(){
        int count = 3;
        Scanner sc = new Scanner(System.in);
        while (count != 0){
            System.out.println("请输入你的密码:");
            String password = sc.nextLine();
            if (password.equals("666")){
                System.out.println("登陆成功!");
                return;
            }else {
                System.out.println("输入错误,你还有 "+count+" 次机会!");
            }
        }
    }

3、模块化解题思路更清晰

例如求1!+2!+3!+4!+…
划分模块,分工合作,调用很方便

    /**
     *
     * n!
     * @param n
     * @return
     */
    public static int suv(int n){
        int result = 1;
        for (int i = 1; i <= n; i++) {
            result *=i;
        }
        return result;
    }

    /**
     *
     * @param x = 5 ---> 1!+2!+3!+4!+5!
     * @return
     */
    public static int suvHaha(int x){
        int sun = 0;
        for (int i = 1; i <= x ; i++) {
            sun +=suv(i);
        }
        return sun;
    }


    public static void main(String[] args) {
        System.out.println(suvHaha(2));
    }

4、给大佬递个归

简单描述:

  1. 调用自己本身
  2. 有一个趋近于终止的条件
  3. 写递归需要有一个提前推导他的递推公式

举个例子:

    public static void func(int n){
        if ( n == 1){
            System.out.println(n);
            return;
        }
        func(n-1);
        System.out.println(n);
    }
    public static void main(String[] args) {
        func(10);
    }
    运行结果:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    Process finished with exit code 0

递的过程:
在这里插入图片描述

归的过程:
在这里插入图片描述

这里第一个执行的打印时:

        if ( n == 1){
            System.out.println(n);
            return;
        }

这里面的System.out.println(n); 是第一个执行的 从这里开始归
然后反复 倒着 执行下面的

        func(n-1);
        System.out.println(n); 

直至结束

3.1、递归求N的阶乘

我们需要找到终止条件,也就是递的终止,归的起始条件 n == 1
这里先求一个 5!

    public static int fun(int n){
       if (n == 1){
           return 1;
       } 
       int sum = n * fun(n-1);
       return sum;
   }
   public static void main(String[] args) {
       System.out.println(fun(5));
   }

不难看出 终止条件(起始条件) n == 1

递的过程:

    int sum = n * fun(n-1);

3.2、按顺序打印数字的每一位

    public static void suv(int n){
        if (n < 10) {
            System.out.println(n);
            return;
        }
        suv(n/10);
        System.out.println(n%10);
    }

    public static void main(String[] args) {
        suv(12345);
    }
    运行结果:
    1
    2
    3
    4
    5

Process finished with exit code 0

再来一个输出不换行:

    public static void suv(int n){
        if (n < 10) {
            System.out.print(n + " ");
            return;
        }
        suv(n/10);
        System.out.print(n%10 + " ");
    }

    public static void main(String[] args) {
        suv(12345);
    }
    运行结果:
    1 2 3 4 5 
    Process finished with exit code 0

3.3、加法递归计算

1+2+3+…

    public static int sun(int n){
        if (n == 1){
            return 1;
        }
        int ret = n+sun(n-1);
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(sun(10));
    }

3.4、计算一个数中组成他的数字之和

    public static int timi(int n){
        if (n<10){
            return n;
        }
        int meat = timi(n/10) + n%10;
        return meat;
    }

    public static void main(String[] args) {
        System.out.println(timi(243));
    }
    运行结果:
    9
    Process finished with exit code 0

3.5、递归打印数组

这里是正序打印:

    public static void print(int[] array,int n) {
        if (n == 1){
            System.out.println(array[n-1]);
            return;
        }
        print(array,n-1);
        System.out.println(array[n-1]);
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,55};
        print(array,array.length);
    }

这里是逆序打印:

    public static void main5(String[] args) {
        System.out.println(timi(243));
    }

    public static void print(int[] array,int n) {
        if (n == 1){
            System.out.println(array[n-1]);
            return;
        }
        System.out.println(array[n-1]);
        print(array,n-1);
    }
    运行代码:
    55
    4
    3
    2
    1
    Process finished with exit code 0

把代码顺序调换就欧克了:

        System.out.println(array[n-1]);
        print(array,n-1);

3.6、斐波那契数列

0 1 1 2 3 5 8 13 21 34 …
递推关系:
在这里插入图片描述

    public static int fuc(int n){
        if (n == 1){
            return 0;
        }
        if (n == 2){
            return 1;
        }
        int rmp = fuc(n-2)+fuc(n-1);
        return rmp;
    }

    public static void main(String[] args) {
        System.out.println(fuc(3));
        System.out.println(fuc(5));
        System.out.println(fuc(44));
    }

但是运行的时候,我发现 System.out.println(fuc(44)); 这里40以后速率很慢
分析发现: 递归由上到下 计算步骤非常复杂,重复运算率很高
在这里插入图片描述
试试循环做法:

    public static int swm(int n){
        if (n == 1){
            return 0;
        }
        if (n == 2){
            return 1;
        }
        int f1 = 0;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <= n; i++) {
            f3 = f1 +f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }

    public static void main(String[] args) {
        System.out.println(swm(1));
        System.out.println(swm(2));
        System.out.println(swm(3));
        System.out.println(swm(5));
        System.out.println(swm(44));
    }
    运行代码:
    0
    1
    1
    3
    433494437

    Process finished with exit code 0

这就秒出结果,十分人性化!!!

4、复习实参与形参问题

思考一下如下代码:

    public static void fuc1(int[]array){
        array = new int[]{1,2,3};
    }
    public static void fuc2(int[]array){
        array[0]=99;
    }
    public static void main(String[] args) {
        int[] array={5,6,7};
        fuc1(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();

        fuc2(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    运行结果:
    5 6 7 
    99 6 7 
    Process finished with exit code 0

由此可以看出,形参如果不指定实参,只会做无用功。

  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值