While 的基础结构

While 的基础结构
public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1-100
        int i = 0;
        while (i < 100){
            i++;
            System.out.println(i);
        }
    }
}
public class WhileDemo02 {
    public static void main(String[] args) {
        //计算1+2+3+...+100=? while 先判断再循环
        int i = 0;
        int sum = 0;
        while (i<100){
            i++;
            sum += i;
        }
        System.out.println("结果为:" + sum);

        //DO while 先循环一次,再进行判断
        int a = 0;
        int total = 0;
        do {
            a++;
            total += a;
        }while (a < 100);
        System.out.println("结果为:" + total);
    }
}
posted @ 2025-01-13 11:00  EndeavorX  阅读(14)  评论(0)    收藏  举报