zoukankan      html  css  js  c++  java
  • java笔记10之循环

    关于for
        循环语句:for循环,while循环,do...while循环。
        
        for循环格式:
            for(初始化语句;判断条件语句;控制条件语句) {
                循环体语句;
            }
            
            执行流程:
                A:执行初始化语句
                B:执行判断条件语句,看其返回值是true还是false
                    如果是true,就继续执行
                    如果是false,就结束循环
                C:执行循环体语句;
                D:执行控制条件语句
                E:回到B继续。
                
        注意事项:
            A:判断条件语句无论简单还是复杂结果是boolean类型。
            B:循环体语句如果是一条语句,大括号可以省略;如果是多条语句,大括号不能省略。建议永远不要省略。
            C:一般来说:有左大括号就没有分号,有分号就没有左大括号

    例子for求和

     1 class ForDemo3 {
     2     public static void main(String[] args) {
     3         //原始做法
     4         System.out.println(1+2+3+4+5+6+7+8+9+10);
     5         
     6         //定义第一个加数
     7         int sum = 0;
     8         
     9         for(int x=1; x<=10; x++) {
    10             //这里的x其实是第二个加数
    11             sum = sum + x;
    12             /*
    13                 0 + 1 = 1
    14                         1 + 2 = 3
    15                                 3 + 3 = 6
    16                                 ...
    17             */
    18             
    19             //sum += x;
    20         }
    21         
    22         System.out.println("sum:"+sum);
    23     }
    24 }

    练习2
            请在控制台输出满足如下条件的五位数
            个位等于万位
            十位等于千位
            个位+十位+千位+万位=百位
            
        分析:
            A:五位数就告诉了我们范围。
            B:分解每一个五位数的个,十,百,千,万位上的数据
            C:按照要求进行判断即可

     1 class ForDemo7 {
     2     public static void main(String[] args) {
     3         //五位数就告诉了我们范围。
     4         for(int x=10000; x<100000; x++) {
     5             //分解每一个五位数的个,十,百,千,万位上的数据
     6             int ge = x%10;
     7             int shi = x/10%10;
     8             int bai  = x/10/10%10;
     9             int qian = x/10/10/10%10;
    10             int wan = x/10/10/10/10%10;
    11             
    12             //按照要求进行判断即可
    13             if((ge==wan) && (shi==qian) && (ge+shi+qian+wan==bai)) {
    14                 System.out.println(x);
    15             }
    16         }
    17     }
    18 }

    例子3 水仙花

     1 /*
     2     需求:统计”水仙花数”共有多少个
     3     
     4     分析:
     5         A:首先必须知道什么是水仙花数
     6             所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
     7             举例:153就是一个水仙花数。
     8             153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
     9         B:定义统计变量,初始化值是0
    10         C:三位数告诉了我们范围,用for循环就可以搞定
    11         D:获取每一个三位数的个,十,百的数据
    12         E:按照要求进行判断
    13         F:如果满足要求就计数。
    14 */
    15 class ForDemo8 {
    16     public static void main(String[] args) {
    17         //定义统计变量,初始化值是0
    18         int count = 0;
    19         
    20         //三位数告诉了我们范围,用for循环就可以搞定
    21         for(int x=100; x<1000; x++) {
    22             //获取每一个三位数的个,十,百的数据
    23             int ge = x%10;
    24             int shi = x/10%10;
    25             int bai = x/10/10%10;
    26             
    27             //按照要求进行判断
    28             if(x == (ge*ge*ge+shi*shi*shi+bai*bai*bai)) {
    29                 //如果满足要求就计数。
    30                 count++;
    31             }
    32         }
    33         
    34         System.out.println("水仙花数共有"+count+"个");
    35     }
    36 }

    关于while

    while循环的基本格式:
            while(判断条件语句) {
                循环体语句;
            }
            扩展格式:
            初始化语句;
            while(判断条件语句) {
                 循环体语句;
                 控制条件语句;
            }
            
            通过这个格式,我们就可以看到其实和for循环是差不多的。
            
            for(初始化语句;判断条件语句;控制条件语句) {
                循环体语句;
            }

    关于do。。。。while

    do...while循环的基本格式:
            do {
                循环体语句;
            }while(判断条件语句);
            
            扩展格式;
            初始化语句;
            do {
                循环体语句;
                控制条件语句;
            }while(判断条件语句);

    注意死循环:
            A:一定要注意控制条件语句控制的那个变量的问题,不要弄丢了,否则就容易死循环。
            B:两种最简单的死循环格式
                while(true){...}
                for(;;){...}

    关于循环嵌套

    例子1

     1 /*
     2     需求:请输出一个4行5列的星星(*)图案。
     3     结果:
     4         *****
     5         *****
     6         *****
     7         *****
     8         
     9     循环嵌套:就是循环语句的循环体本身是一个循环语句。
    10     
    11     通过结果我们知道这样的一个结论:
    12         外循环控制行数
    13         内循环控制列数
    14 */
    15 class ForForDemo {
    16     public static void main(String[] args) {
    17         //原始做法
    18         System.out.println("*****");
    19         System.out.println("*****");
    20         System.out.println("*****");
    21         System.out.println("*****");
    22         System.out.println("-------------");
    23         
    24         //虽然可以完成需求,但是不是很好
    25         //如果是多行多列就会比较麻烦
    26         //所以我们准备改进
    27         //如何改进呢?
    28         //我先考虑如何实现一行*的问题
    29         //System.out.println("*****");
    30         //我们要想的是如何实现一次输出一颗*的问题
    31         //System.out.println("*");
    32         //System.out.println("*");
    33         //现在虽然可以一次一颗*,但是却换行了,我要求不能换行,怎么办呢?
    34         //输出语句的另一种格式:System.out.print(); 这个是不带换行的
    35         //System.out.print("*");
    36         //System.out.print("*");
    37         //System.out.print("*");
    38         //System.out.print("*");
    39         //System.out.print("*");
    40         //如果我要在一行上打出多颗*,比较麻烦,而代码是重复的,所以我决定用循环改进
    41         for(int x=0; x<5; x++) {
    42             System.out.print("*");
    43         }
    44         //我们可以通过空的输出语句实现换行:System.out.println();
    45         System.out.println();
    46         
    47         //既然我可以打出一行,我就可以打出第二行
    48         for(int x=0; x<5; x++) {
    49             System.out.print("*");
    50         }
    51         //我们可以通过空的输出语句实现换行:System.out.println();
    52         System.out.println();
    53     
    54         //同理打出第三行,第四行
    55         for(int x=0; x<5; x++) {
    56             System.out.print("*");
    57         }
    58         //我们可以通过空的输出语句实现换行:System.out.println();
    59         System.out.println();
    60         
    61         //既然我可以打出一行,我就可以打出第二行
    62         for(int x=0; x<5; x++) {
    63             System.out.print("*");
    64         }
    65         //我们可以通过空的输出语句实现换行:System.out.println();
    66         System.out.println();
    67         System.out.println("-----------------");
    68         //同样的代码出现了4次,说明我们程序写的不好,用循环改进
    69         for(int y=0; y<4; y++) {
    70             for(int x=0; x<5; x++) {
    71                 System.out.print("*");
    72             }
    73             //我们可以通过空的输出语句实现换行:System.out.println();
    74             System.out.println();
    75         }
    76     }
    77 }

    例子2 99乘法表

     1 class ForForDemo3 {
     2     public static void main(String[] args) {
     3         for(int x=0; x<9; x++) {
     4             for(int y=0; y<=x; y++) {
     5                 System.out.print("*");
     6             }
     7             System.out.println();
     8         }
     9         System.out.println("--------------");
    10         //为了使用数据,我们从1开始
    11         for(int x=1; x<=9; x++) {
    12             for(int y=1; y<=x; y++) {
    13                 System.out.print(y+"*"+x+"="+y*x+"	");
    14             }
    15             System.out.println();
    16         }
    17     }
    18 }

    关于跳转语句

    控制跳转语句:
            break:中断
            continue:继续  跳出一次循环,进入下一次的执行
            return:返回   其实它的作用不是结束循环的,而是结束方法的
        break:中断的意思
        使用场景:
            A:switch语句中
            B:循环语句中。
                (循环语句中加入了if判断的情况)
            注意:离开上面的两个场景,无意义。
            
        如何使用呢?
            A:跳出单层循环
            B:跳出多层循环
                要想实现这个效果,就必须知道一个东西。带标签的语句。
                格式:
                    标签名: 语句
    例子1

     1 class ContinueDemo {
     2     public static void main(String[] args) {
     3         for(int x=0; x<10; x++) {
     4             if(x == 3) {
     5                 //break;
     6                 continue;
     7             }
     8             
     9             System.out.println(x);
    10         }
    11     }
    12 }

    例子2

     1 class ReturnDemo {
     2     public static void main(String[] args) {
     3         for(int x=0; x<10; x++) {
     4             if(x == 2) {
     5                 System.out.println("退出");
     6                 //break;
     7                 //continue;
     8                 return;
     9             }
    10             
    11             System.out.println(x);
    12         }
    13         
    14         System.out.println("over");
    15     }
    16 }
  • 相关阅读:
    MFC学习篇(二):error LNK2005 及其解决方法
    MFC学习篇(一):用OpenCV显示视频
    记一次mysql安装!
    常用数据对应关系及简单介绍
    docker
    月份及星期 缩写
    java 面对对象笔记
    linux小案例 定时备份数据库
    rpm_yum_开发工具的安装
    shell入门
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/6266758.html
Copyright © 2011-2022 走看看