java流程控制:循环结构

2021/9/3 22:07:20

本文主要是介绍java流程控制:循环结构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

循环结构

  • while循环
  • do while循环
  • for循环
  • jdk5中引入了一种主要用于数组的增强for循环

while

  • while循环是最基本的循环,他的结构为:

    while(布尔表达式){
    //循环内容
    }
    
  • 只要布尔表达式为true,循环就会一直执行下去

  • 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环

  • 少部分情况需要循环一直执行,比如服务器的请求响应等

  • 循环条件一直为true会造成无限循环(死循环),我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!

package com.ljh.structure;

/**
 * while循环
 */
public class WhileDemo1 {
    public static void main(String[] args) {
        //输出1-100的所有数字
        int num=0;
        while (num<100){
            num++;
            System.out.println(num);
        }
        //求出1+2+3+4+5......+100的总和
        int he=0;
        int i=0;
        while (i<100){
            i++;
            he+=i;
        }
        System.out.println("总和为:"+he);
    }
}

do while

  • 对于while语句而言,如果不满足条件,则不能进入循环。但是有时候我们需要及时不满足条件,也至少执行一次

  • do...while循环和while循环相似,不同的是,do...while循环至少会执行一次

  • 语句:

    do{
        //代码语句
    }while(布尔表达式);
    
  • while和do...while的区别:

    • while先判断后执行,dowhile是先执行后判断
    • do...while总是保证循环体至少会被执行一次!这就是主要差别
package com.ljh.structure;

/**
 * do while
 */
public class DoWhileDemo1 {
    public static void main(String[] args) {
        //使用 do while输出1+2+3+4+5......+100的总和
        int num=0;
        int count=0;
        do {
            count++;
            num+=count;
        }while (count<100);
        System.out.println(num);
    }
}
package com.ljh.structure;

public class DoWhileDemo2 {
    public static void main(String[] args) {
        //do while while区别
        //do while中的方法体至少会被执行一次
        int x=0;
        do {
            System.out.println(x);
        }while (x<0);
        System.out.println("============================");
        while (x<0){
            System.out.println(x);
        }
    }
}

for循环

  • 虽然所有循环结构都可以用while或者do...while表示,但Java提供了另外一种语句------for循环,使一些循环结构变得更加简单

  • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构

  • for循环执行的次数是在执行前就确定的,语法格式如下:

    for(初始化;布尔表达式;更新){
    	//代码语句
    }
    
  • 练习一:计算0到100之间的奇数的和 和偶数的和

    package com.ljh.structure;
    
    public class ForDemo1 {
        public static void main(String[] args) {
            //计算0-100之间的所有奇数和偶数的和
            int jiNum=0;
            int ouNum=0;
            for (int x=0;x<=100;x++){
                if (x%2==0){
                    ouNum+=x;
                }else {
                    jiNum+=x;
                }
            }
            System.out.println("奇数和:"+jiNum);
            System.out.println("偶数和:"+ouNum);
        }
    }
    
    
  • 练习二:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

    package com.ljh.structure;
    
    /**
     * 用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
     */
    public class ForDemo3 {
        public static void main(String[] args) {
            int count=0;
            for (int x=1;x<=1000;x++){
                if (x%5==0){
                    System.out.print(x+"\t");
                    count++;
                    if (count%3==0){
                        System.out.println();
                    }
                }
            }
            System.out.println();
            System.out.println("=======================================");
            for (int x=1;x<=1000;x++){
                if (x%5==0){
                    System.out.print(x+"\t");
                }
                if (x%(5*3)==0){
                    System.out.println();
                }
            }
        }
    }
    
    package com.ljh.structure;
    
    /**
     * 用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
     */
    public class ForDemo2 {
        public static void main(String[] args) {
            int x=1;
            int count=0;
            while (x<=1000){
                if (x%5==0){
                    count++;
                    System.out.print(x+"\t");
                    if (count%3==0){
                        System.out.println();
                    }
                }
                x++;
            }
            System.out.println();
            System.out.println("=============================================");
            int y=1;
            while (y<=1000){
                if (y%5==0){
                    System.out.print(y+"\t");
                }
                if (y%(5*3)==0){
                    System.out.println();
                }
                y++;
            }
        }
    }
    
    package com.ljh.structure;
    /**
     * 用do...while循环输出1-1000之间能被5整除的数,并且每行输出3个
     */
    public class ForDemo5 {
        public static void main(String[] args) {
            int x=1;
            do {
                if (x%5==0){
                    System.out.print(x+"\t");
                }
                if (x%(5*3)==0){
                    System.out.println();
                }
                x++;
            }while (x<=1000);
        }
    }
    
  • 练习三:打印九九乘法表

    package com.ljh.structure;
    
    /**
     * for循环实现九九乘法表
     */
    public class ForDemo4 {
        public static void main(String[] args) {
            for (int x=1;x<=9;x++){
                for (int y=1;y<=x;y++){
                    System.out.print(y+"*"+x+"="+y*x+"\t");
                }
                System.out.println();
            }
        }
    }
    
    package com.ljh.structure;
    
    public class ForDemo6 {
        public static void main(String[] args) {
            //while实现99乘法表
            int x=1;
            while (x<=9){
                int y=1;
              while (y<=x){
                  System.out.print(y+"*"+x+"="+y*x+"\t");
                  y++;
              }
              x++;
                System.out.println();
            }
        }
    }
    
    package com.ljh.structure;
    
    public class ForDemo7 {
        public static void main(String[] args) {
            //do...while实现99乘法表
            int x=1;
            do {
                int y=1;
                while (y<=x){
                    System.out.print(y+"*"+x+"="+y*x+"\t");
                    y++;
                }
                System.out.println();
                x++;
            }while (x<=9);
        }
    }
    

增强for循环

  • 在这里先做个了解,之后数组在详细讲

  • java5引入了一种主要用于数组或集合的增强型for循环

  • java增强for循环语法格式:

    for(声明语句 : 表达式){
    	//代码语句
    }
    
  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配,其作用域限定在循环语句块中,其值与此时数组元素的值相等

  • 表达式:表达式是要访问的数组名称,或者是返回值为数组的方法

package com.ljh.structure;

/**
 * 增强for循环
 */
public class ForDemo7 {
    public static void main(String[] args) {
        int[] numArr={1,2,3,4,5};
        System.out.println("================for循环遍历================");
        for (int x=0;x<numArr.length;x++){
            System.out.print(numArr[x]+"\t");
        }
        System.out.println();
        System.out.println("================增强for循环遍历================");
        for (int num : numArr){
            System.out.print(num+"\t");
        }
    }
}



这篇关于java流程控制:循环结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程