一、while循环

1、语句定义格式:


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


2、while循环可以和for循环等价转换。
      举例:打印10个HelloWorld

public class WhileDemo1 {
    public static void main(String[] args) {
        System.out.println("========for循环打印10句HelloWorld===========");
        for (int i = 1; i <= 10; i++) {
            System.out.println("HelloWorld");
        }

        System.out.println("=======while循环打印10句HelloWorld=============");
        /*
                初始化语句:int i=1;
                判断条件语句:i<=10;
                循环体语句:System.out.println("HelloWorld");
                控制条件语句:i++;
         */
        int i = 1;
        while (i<=10){
            System.out.println("HelloWorld");
            i++;
        }

    }
}

3、求出1~100之和,用while循环实现

public class WhileDemo2 {
    public static void main(String[] args) {
        int sum1 = 0;
        for (int i = 1; i <= 100; i++) {
            sum1 = sum1 + i;
        }
        System.out.println(sum1);


        System.out.println("========while循环改进=================");
        int sum2 = 0;
        int i = 1;
        while (i <= 100) {
            sum2 = sum2 + i;
            i++;
        }
        System.out.println(sum2);

    }
}

4、while循环与for循环的区别?


1、他们之间虽然可以做等价转换,但是开发中如果遇到在一个范围内做循环的时候,优先使用for循环 2、当循环的次数不确定的时候,优先使用while循环,举例:喝水,吃葡萄 作用域带来的区别: 3、for循环结束后,初始化语句中的变量在外部无法访问 4、while循环结束后,初始化语句中的变量还能继续使用


public class WhileDemo3 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum = sum + i;
        }
        System.out.println(sum);
//        System.out.println(i);

        int sum2 = 0;
        int j = 1;
        while (j <= 100) {
            sum2 = sum2 + j;
            j++;
        }
        System.out.println(sum2);
        System.out.println(j);

    }
}

5、我国最高山峰是珠穆朗玛峰:884800m,我现在有一张足够大的纸张,厚度为1m。请问我折叠多少次就可以到不低于珠穆朗玛峰的高度?

public class WhileTest {
    public static void main(String[] args) {
        //定义初始纸的厚度
        int thickness = 1;
        //定义一个变量接收折叠的次数
        int count = 0;

        //由于不知道折叠多少次,我们使用while循环
        while (thickness <= 884800) {
            count++;
            thickness = thickness * 2;
        }
        System.out.println("折叠了" + count + "次,纸的厚度为:" + thickness);

        System.out.println("==============for循环实现============================");
        for(int thickness1 = 1,count1=0;thickness1 <= 884800;){
            thickness1 = thickness1 * 2;
            count1++;
            if(thickness1 >= 884800){
                System.out.println("折叠了" + count1 + "次,纸的厚度为:" + thickness1);
            }

        }



    }
}

二、do.....while循环

1、语句定义格式


(1)基本格式 do { 循环体语句; }while((判断条件语句); (2)扩展格式 初始化语句; do { 循环体语句; 控制条件语句; } while(判断条件语句); 2、while循环与do...while循环的区别? while循环先进行判断条件,看看是不是true,如果是true再执行循环体内容 而do...while循环会先执行一遍循环体内容,然后再去判断条件,看看是不是true,如果是true,继续执行。


public class DoWhileDemo1 {
    public static void main(String[] args) {
        int x = 3;
        while (x<3){
            System.out.println("HelloWorld");
        }

        System.out.println("========do...while循环=============");
        do{
            System.out.println("HelloWorld");
        }while (x<3);

        System.out.println("=========while死循环===================");
        boolean flag = true;
        while (flag){
            System.out.println("HelloWorld");
        }


    }
}

三、循环嵌套


在控制台上打印4行5列的星星* 1、要想知道如何打印4行5列*,那就必须得先知道如何打印一行 2、而我们在此之前我们打印一行相同得数据并没有介绍过,如果想使多个输出语句在一行,就得使用java得另一种输出格式。 System.out.println(); 输出并且换行 System.out.print(); 输出不换行


public class XunHuanQianTaoDemo {
    public static void main(String[] args) {
//        System.out.println("*");
//        System.out.println("*");
//        System.out.println("*");
//        System.out.println("*");
//        System.out.println("*");

        //java输出语句得另一种格式
        //System.out.print();
//        System.out.print("*");
//        System.out.println("*");

        //打印一行5个*
//        System.out.print("*");
//        System.out.print("*");
//        System.out.print("*");
//        System.out.print("*");
//        System.out.print("*");

        //加入制表符打印一行5个*,使*之间存在间隙,更加美观
//        System.out.print("*\t");
//        System.out.print("*\t");
//        System.out.print("*\t");
//        System.out.print("*\t");
//        System.out.print("*\t");

        //使用for循环改进打印一行5个星*
//        for(int i=0;i<5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
//        for(int i=0;i<5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
//        for(int i=0;i<5;i++){
//            System.out.print("*\t");
//        }
//        System.out.println();
//        for(int i=0;i<5;i++){
//            System.out.print("*\t");
//        }

        //用for循环再次改进
        for (int j = 0; j < 4; j++) {  // 外面的循环控制的是行
            for (int i = 0; i < 5; i++) { // 内部的循环控制的是列
                System.out.print("*\t");
            }
            System.out.println(); // 每打印一行后换行
        }

        System.out.println("================================================================");
        /**
         *  需求:请输出如下图形
         *  1、要想输出如下图形,就必须先知道如何输出5行5列,我们刚刚学过
         *  2、通过寻找规律去打印下面图形
         *
         *             *             第一行,i=0,j<=i,打印1个
         *             **            第二行,i=1,j<=i,打印2个
         *             ***           第三行,i=2,j<=i,打印3个
         *             ****          第四行,i=3,j<=i,打印4个
         *             *****         第五行,i=4,j<=i,打印5个
         */

        //先打印5行5列
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print("*\t");
            }
            System.out.println();
        }

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

        System.out.println("==============打印九行九列的星星==============");
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*\t");
            }
            System.out.println();
        }

        System.out.println("=============在控制台输出九九乘法表=================");
        /*
            1*1=1
            2*1=2 2*2=4
            3*1=3 3*2=6 3*3=9
            ...
            9*1=9 9*2=18 .............. 9*9=81
         */
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + (i * j) + "\t");
            }
            System.out.println();
        }

    }
}