While

Demo

while循环:
        语句定义格式1:
            初始化条件语句;
            while(判断条件语句){
               循环体语句;
               控制条件语句;
            }

         语句定义格式2:
            初始化条件语句;
            do{
                循环体语句;
               控制条件语句;
            }while(判断条件语句);
 注意:
        1、while循环可以和for循环等价转换
        2、for循环和while循环的使用场景:
            for循环适合明确的范围内循环  【吃葡萄🍇】
            当循环次数不明确获取就是要求次数的时候,优先考虑while循环【喝水】

public class WhileDemo1 {
    public static void main(String[] args) {
        //输出10行hello world
        for (int i = 1; i < 0; i++) {
            System.out.println("hello world");
        }
        System.out.println("---------------------------");
        int i = 1;
        while (i < 0){
            System.out.println("hello world");
            i++;
        }
        System.out.println("---------------------------");
        int i2 = 1;
        do{
            System.out.println("hello world");
            i2++;
        }while (i2 < 0);
    }
}

Test

/*
    我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。
    请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?


 */
public class WhileTest1 {
    public static void main(String[] args) {
        int high = 884800;
        int thickness = 1;
        int count = 0;
        while (thickness<high){
            thickness *=2;
            count++;
        }

        System.out.println("共折叠 "+count+"次");

        //死循环
//        while (true){
//
//        }
    }
}
posted @   rrrzzzrrr  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示