JAVA/编程语言

[上]JAVA学习系列模块四第五章92.while循环_基本使用

daimafengzi · 5月24日 · 2024年 · · 本文共405个字 · 预计阅读2分钟1054次已读

[上]JAVA学习系列模块四第五章92.while循环_基本使用

视频


笔记

1.格式:
初始化变量;
while(比较){
循环语句;
步进表达式
}

2.执行流程:
a.初始化变量
b.比较,如果是true,就走循环语句,走步进表达式
c.再比较,如果还是true,继续走循环语句,继续走步进表达式
d.再比较,直到比较为false,循环结束

public class Demo01While {
    public static void main(String[] args) {
        int i = 0;
        while(i<5){
            System.out.println("我爱java,我更爱钱");
            i++;
        }
    }
}
public class Demo02While {
    public static void main(String[] args) {
        int sum = 0;
        int i = 1;
        while(i<=3){
           sum+=i;
           i++;
        }
        System.out.println("sum = " + sum);
    }
}
public class Demo03While {
    public static void main(String[] args) {
        int sum = 0;
        int i = 1;
        while (i <= 100) {
            if (i % 2 == 0) {
                sum += i;
            }
            i++;
        }
        System.out.println("sum = " + sum);
    }
}
public class Demo04While {
    public static void main(String[] args) {
        int count = 0;
        int i = 1;
        while (i <= 100) {
            if (i % 2 == 0) {
                count++;
            }
            i++;
        }
        System.out.println("count = " + count);
    }
}
0 条回应
Copyright © 2022-2024 LuoWeiHua
| 耗时 0.347 秒 | 查询 58 次 | 内存 4.14 MB |