java基础:while循环,珠穆拉玛峰案例

发布时间 2023-11-29 16:21:55作者: 小彭先森

1.whille循环格式:

package com.itheima.loop;

public class While1 {
    public static void main(String[] args) {
        int i=0;//初始化语句
        while(i<5){//循环条件
            System.out.println("  ");//循环体语句
            i++;//迭代语句,迭代语句不要丢掉了
        }
    }
}

 

2.珠穆拉玛峰案例:

 

 

package com.itheima.loop;

public class While2 {
    public static void main(String[] args) {
        //实用while循环解决问题
        //1.定义
        double peakHeight=8848860;
        double paperThickness=0.1;
        int i=0;
        while(paperThickness<peakHeight){
            paperThickness *=2;
            i++;
        }
        System.out.println(i);
        System.out.println(paperThickness);
    }
}