• 四、 java循环结构


      for循环结构:
      格式:①初始化条件;②循环条件;③迭代条件;④循环体

    for(①;②;③){
        //
    }

      执行过程:①-②-④-③-②-④-③-。。。-④-③-②,直至循环条件不满足,退出当前的循环。

    class myfor{
            public static void main(String[] args){
                    for(int i = 0;i<4;i++){
                    System.out.println("hello java");
                    }
            }
    }
    root@debian:/home/jeff/java_coding/day004# java myfor 
    hello java
    hello java
    hello java
    hello java

      for循环执行过程

    class myfor1{
            public static void main(String[] args){
                    int j = 1;
                    for(System.out.print('a');j<4;System.out.print('b'),j++){
                    System.out.println('c');
                    }
            }
    }
    root@debian:/home/jeff/java_coding/day004# java myfor1 
    ac
    bc
    bc

      用for循环解决问题

    //100以内偶数的和
    class myfor2{
            public static void main(String[] args){
                    int sum = 0;
                    for(int i = 1;i<101;i++){
                            if(i % 2 ==0){
                                    sum +=i;
                            }
                    }
                    System.out.println(sum);
            }
    }
    root@debian:/home/jeff/java_coding/day004# java myfor2 
    2550

      水仙花数问题

    /*
    输出所有的水仙花数,所谓水仙花数是指一个3
       位数,其各个位上数字立方和等于其本身。
        例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
    */
    class shuixianhua{
        public static void main(String[] args){
            for(int i = 100;i<1000;i++){
                int bai = i / 100;
                int shi = (i %100) /10;
                int ge = i %10;
                
                if(i == bai*bai*bai+shi*shi*shi+ge*ge*ge){
                    System.out.println(i);
                }
            }
        }
    }
    root@debian:/home/jeff/java_coding/day004# java shuixianhua 
    153
    370
    371
    407

      while循环

      格式:

    while(②){
        ④
        ③
    }

      for循环与while可以相互转换。

    class myfor3{
        public static void main(String[] args){
            //100以内的和
            int i = 1;
            int sum = 0;
            while(i <= 100){
                if(I % 2 == 0){
                    System.out.println(i);
                    sum += i;
                }
                i++;
            }
            System.out.println(sum);
        }
    }

      do-while

      格式:①初始化条件;②循环条件;③迭代条件;④循环体

    do{
        ④
        ③
    }while(②);

      do-while与while类似,唯一的区别就是do-while至少会执行一次。

    class mywhile{
        public static void main(String[] args) {
            //do-while与while的区别
            int i = 10;
            int j = 10;
            do{
                System.out.println(i);
                i++;
            }while(i < 10);
    
    
            while(j < 10){
                System.out.println(j);
                j++;
            }
        }
    }

      运行会打印一个10,即i先do一次打印才会结束循环体。

      无限循环:for(;;){}或者
      while(true){}
      说明:一般情况下,在无限循环内部要有程序终止的语句,使用break实现。若没有,那就是死循环!

      死循环应用

    //键入随意个数字,0退出,输出正数与负数的个数
    import java.util.Scanner;
    class deadforloop{
        public static void main(String[] args){
            Scanner s = new Scanner(System.in);
            int a = 0;//记录正数的个数
            int b = 0;//记录负数的个数
            //for(;;){
            while(true){
                System.out.println("请输入一个整数:");
                int num = s.nextInt();
                if(num > 0)
                    a++;
                else if(num < 0)
                    b++;
                else
                    break;    
            }
            System.out.println("正数的个数为:" + a);
            System.out.println("负数的个数为:" + b);
            }
        }
    root@debian:/home/jeff/java_coding/day004# java deadforloop 
    请输入一个整数:
    12
    请输入一个整数:
    11
    请输入一个整数:
    0
    正数的个数为:2
    负数的个数为:0

      循环嵌套

    嵌套循环:循环中还可以声明循环。相当于内层循环的整体充当外层循环的循环体
    for(;;){
        for(;;){
            
        }
    }
    while(){
        for(;;){}
    }

       循环之打印各种姿势的*

    class mystar{
        public static void main(String[] args){
            for(int i = 0;i<4;i++){//外层循环
                for(int j = 0;j<5;j++){//内层循环
                    System.out.print("*");
                }
                System.out.println();//每层换行
            }
        }
    }

      运行结果:

    root@debian:/home/jeff/java_coding/day004# java mystar 
    *****
    *****
    *****
    *****
    class mystar1{
        public static void main(String[] args){
            for(int i = 1;i<5;i++){//外层循环
                for(int j = 0;j<i;j++){//内层循环
                    System.out.print("*");
                }
                System.out.println();//每层换行
            }
        }
    }

      运行结果:

    root@debian:/home/jeff/java_coding/day004# java mystar1 
    *
    **
    ***
    ****
    class mystar2{
        public static void main(String[] args){
            for(int i = 4;i>0;i--){//外层循环
                for(int j = 0;j<i;j++){//内层循环
                    System.out.print("*");
                }
                System.out.println();//每层换行
            }
        }
    }

      运行结果:

    root@debian:/home/jeff/java_coding/day004# java mystar2 
    ****
    ***
    **
    *
    class mystar3{
        public static void main(String[] args){
            for(int i = 1;i<6;i++){//外层循环
                for(int j = 0;j<i;j++){//内层循环
                    System.out.print("*");
                }
                System.out.println();//每层换行
                
            }
            for(int m = 4;m>0;m--){//外层循环
                for(int n = 0;n<m;n++){//内层循环
                    System.out.print("*");
                }
                System.out.println();//每层换行
            }
        }
    }

      运行结果:

    root@debian:/home/jeff/java_coding/day004# java mystar3 
    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *
    class mystar4{
        public static void main(String[] args){
            for(int i = 1;i<6;i++){//外层循环
                for(int j = 0;j<5-i;j++){//内层循环
                    System.out.print(" ");
                }
                for(int j = 0;j<i;j++){//内层循环
                    System.out.print("* ");
                }
                System.out.println();//每层换行
            }
            for(int i = 4;i>0;i--){//外层循环
                for(int j = 1;j<6-i;j++){//内层循环
                    System.out.print(" ");
                }
                for(int j = 0;j<i;j++){//内层循环
                    System.out.print("* ");
                }
                System.out.println();//每层换行
            }
        }
    }

      运行结果:

    root@debian:/home/jeff/java_coding/day004# java mystar4 
        * 
       * * 
      * * * 
     * * * * 
    * * * * * 
     * * * * 
      * * * 
       * * 
        * 

      乘法口诀

    class chengfabiao{
        public static void main(String[] args){
            for(int i = 1;i<10;i++){//外层循环
                for(int j = 1;j<=i;j++){//内层循环
                    System.out.print(i+"*"+j+"="+i*j+" ");
                }
                System.out.println();//每层换行
            }
        }
    }

      运行结果:

    root@debian:/home/jeff/java_coding/day004# java chengfabiao 
    1*1=1 
    2*1=2 2*2=4 
    3*1=3 3*2=6 3*3=9 
    4*1=4 4*2=8 4*3=12 4*4=16 
    5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 
    6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
    7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
    8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
    9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 

       求100000以内的质数:

    /*
    100以内的所有质数*/
    class zhishu{
        public static void main(String[] args){
            boolean flag = false;
            long start = System.currentTimeMillis();//获取系统当前的毫秒数
            for(int i = 2;i <= 100000;i++){//实现100以内的自然数的遍历
                //如何判断i是否为一个质数
                for(int j = 2;j <= Math.sqrt(i);j++){
                    if(i % j == 0){
                        flag = true;
                        break;
                    }
                }
                if(!flag){//if(flag == false){
                    System.out.println(i);
                }
                flag = false;
            }
            long end = System.currentTimeMillis();
            System.out.println("所花费的时间为:" + (end - start));
        }
    }

      花费时间103毫秒。

      break结束当前循环,continue结束当次循环。其他语言也是这样使用的。在break和continue语句之后不能添加其他语句,因为永远也不可能被执行!

      break与continue跳出多层的最外层:

    class breakcontinue{
        public static void main(String[] args) {
                label:for(int i = 1;i < 5;i++){
                for(int j = 1;j <= 10;j++){
                    if(j % 4 == 0){
                        //break;
                        //continue;
                        break label;
                        //continue label;//跳出此标签的本次循环,此处时最外层的本次循环
                    }
                    System.out.print(j);
                }
                System.out.println();
            }
        }
    }

      用此方法求100000以内的质数。

    class zhishu1{
        public static void main(String[] args){
            long start = System.currentTimeMillis();//获取系统当前的毫秒数
            l:for(int i = 2;i <= 100000;i++){//实现100以内的自然数的遍历
                //如何判断i是否为一个质数
                for(int j = 2;j <= Math.sqrt(i);j++){
                    if(i % j == 0){
                        //一旦被其他数整除就跳出最外层的循环测试下一个数据;
                        continue l;
                    }
                }
                    System.out.println(i);
            }
            long end = System.currentTimeMillis();
            System.out.println("所花费的时间为:" + (end - start));
        }
    }

       

  • 相关阅读:
    GUID
    ORA-04044: 此处不允许过程, 函数, 程序包或类型和
    去掉word文档两边的空白
    Mysql数据库服务启动
    计算两个日期之间的天数
    SpringMVC——接收请求参数和页面传参
    ajax中get和post区别
    如何实现两个页面之间进行传值
    面试题
    MySQL数据库优化
  • 原文地址:https://www.cnblogs.com/Jeffding/p/8663567.html
Copyright © 2020-2023  润新知