Whlie

发布时间 2023-05-28 11:43:20作者: Karlshell
package com.karl;

public class WhileDemo {
    public static void main(String[] args) {
        //功能上和for完全一样,for能解决的while也能解决反之亦然
        //使用规范:知道循环几次用for,不知道循环几次建议使用while
        /**
         * 初始化语句
         * while(循环条件){
         *     循环体语句(被重复执行的代码);
         *     迭代语句;
         * }
         */
        int i = 0;
        while (i<5){
            //0 1 2 3 4
            System.out.println("王大锤");
            i++;
        }


        System.out.println("___________________________________");


        //定义山峰高度和纸张高度
        double gao=8848860;
        double zhi=0.1;
        //定义变量来记录折叠多少次
        int count=0;
        //定义while循环,循环条件(纸张厚度<山峰高度)
        while (zhi<gao){
            //把纸张厚度*2,然后返回到纸张上
            zhi=zhi*2;
            count++;
        }
        System.out.println("需要折叠多少次:"+count);
        System.out.println("纸张的最后厚度:"+zhi);

        System.out.println("___________________________________");

//其他区别:for循环中,控制循环的变量只在循环中使用。while循环中,控制循环的变量在循环后还可以继续使用
        for (int j = 0; j < 3; j++) {
            System.out.println("huhuhuhuhh");
        }



        int m=0;
        while (m<3){
            System.out.println("kokokoo");
            m++;
        }
        System.out.println(m);
    }




}