我是靠谱客的博主 称心汉堡,这篇文章主要介绍Java笔记——流程控制LOOP循环,现在分享给大家,希望可以做个参考。

BreakAndContinue

	目标:理解break和continue的作用
	场景1:假如你又有老婆了,然后你犯错了,你老婆罚你做五天家务,每天都是洗碗,但是第三天她心软了,原谅你了
	contiune 跳出当前循环的当次执行,进入循环的下一次
    场景2:假如你又有老婆了,然后你犯错了,你老婆罚你做五天家务,每天都是洗碗,但是第三天她心软了,原谅你了,但是依然不解恨,第4,5天依然洗
public class BreakAndContinue {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println("快乐的洗碗");
            if (i == 2) {
                System.out.println("你老婆心软了,原谅你了");
                break;
            }
        }
        System.out.println("------------------------");
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                System.out.println("心软了,今天不洗");
                continue;
            }
            System.out.println("继续快乐洗碗"+i);
        }

    }
}

DeadForDemo8——死循环

public class DeadForDemo8 {
    public static void main(String[] args) {
        System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
        //定义一个正确密码
        int okPassword = 520;
        //创建一个输入对象
        Scanner sc = new Scanner(System.in);
        //定义一个死循环让用户不断输入密码进行验证
        while (true){
            System.out.println("请输入正确的密码:");
            int password = sc.nextInt();
            if (password == okPassword) {
                System.out.println("登录成功了~~~~~");
                break;//可以理解结束当前的死循环
            }else{
                System.out.println("密码错误");
            }
        }
    }
}

在这里插入图片描述

DoWhile

目标:学会使用dowhile循环,并理解其执行流程

public class DoWhileDemo7 {
    public static void main(String[] args) {
        int i = 0;
        do {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
            i++;
        }while (i < 3);

        System.out.println("-------------------------");
        for (int j = 0; j < 10; j++) {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
        }

        int n = 0;
        do {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
            n++;
        }while (n < 3);
        System.out.println(n);
    }
}

三种循环的区别:
for循环和while循环(先判断后执行)
do···while(第一次先执行后判断)
for和while的区别
for循环和while循环的执行流程是一模一样
如果已知循环次数建议使用for循环,如果不清楚要循环多少次建议使用while循环
for循环中,控制循环的变量只在循环中可以使用,while循环中,控制循环的变量在循环后还可以继续使用

For

public class ForDemo1 {
    public static void main(String[] args) {
        for (int i = 0; i < 3;i++) {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
        }
        System.out.println("-----------------------------");
        for (int i = 0; i < 5;i++) {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
        }
        System.out.println("-----------------------------");
        for (int i = 1; i < 5;i++) {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
        }
        System.out.println("-----------------------------");
        for (int i = 1; i <= 5;i++) {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
        }
        System.out.println("-----------------------------");
        for (int i = 1; i <= 5;i+=2) {
            System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
        }
    }
}

ForFor

	目标:理解嵌套循环的执行流程
    场景:假如你有老婆,然后你犯错了,你老婆罚你说5天,每天三句我爱你
public class ForForDemo9 {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("I donnt wanna wake up from this sweet sweet dream.--Enkidu");
            }
            System.out.println("-----------------");
        }

        /*
            *****
            *****
            *****
            *****
        */
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

在这里插入图片描述

ForTest2

	需求:求1-5之间的数据和,并把结果输出
    分析:for循环能够依次访问到1,2,3,4,5
    使用:sum求和
public class ForTest2 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 5; i++) {
            //i = 1,2,3,4,5
            sum += i;//等价于:sum = sum + i;
        }
            System.out.println("一到五的和"+ sum);
    }
}

在这里插入图片描述

最后

以上就是称心汉堡最近收集整理的关于Java笔记——流程控制LOOP循环的全部内容,更多相关Java笔记——流程控制LOOP循环内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部