编程语言
首页 > 编程语言> > Java的while 与 do…while

Java的while 与 do…while

作者:互联网

1.while循环

例1:while 与 do…while 的区别

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        //先判断a是否小于0,再输出a
        while (a < 0){
            System.out.println(a);
            a++;
        }
        System.out.println("____________________");
        //先输出a,再判断a是否小于0
        do {
            System.out.println(a);
            a++;
        }while(a < 0);
    }
}

运行结果

例2:输出1+2+3+……+100

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do {
            sum += i;
            i++;
        }while (i <= 100);
        System.out.println(sum);
    }
}

标签:do,Java,int,System,while,循坏,public
来源: https://www.cnblogs.com/l574/p/15026384.html