15

流程控制3

循环结构

Java5中新增了一个增强版的for循环

while循环

public class WhileDemo01 {
    public static void main(String[] args) {
//        输入1-100
        int i =0;
        while (i<100){//布尔表达式
            i++;//循环内容
            System.out.println(i);//循环内容
        }

    }
}

计算1+2+3...+100的和?

public class WhileDemo03 {
    public static void main(String[] args) {
//        计算1+2+3...+100的和?
        int i = 0;
        int sum =0;
        while (i<=100){
            sum=sum+i;
            i++;
        }
        System.out.println(sum);
    }
}

死循环

public class WhileDemo02 {
    public static void main(String[] args) {
        while (true){
//            死循环
//            应该尽量避免这种情况出现
        }
    }
}

do...while循环

  • 对于while而言,如果条件不满足,则不能进入循环。但有时需要即使不满足条件,也至少执行一次。

  • do...while循环和while循环相似,不同的是do...while至少执行一次

  • do...while是先执行后判断,while是先判断后执行

    public class DoWhileDemo01 {
        public static void main(String[] args) {
            int i =0;
            int sum=0;
            do {
                sum=sum+i;
                i++;
            }while (i<=100);
            System.out.println(sum);
        }
    }
    

while和do...while的区别

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int i =0;
        do {
            i++;
            System.out.println(i);
        }while (i<0);
        System.out.println("===========");
        while (i<0){
            i++;
            System.out.println(i);
        }
    }
}

for循环

public class ForDemo01 {
    public static void main(String[] args) {
//           初始化值  ;判断;迭代
        for (int i = 0; i < 100; i++) {
//            循环体
            System.out.println(i);
            /*
            关于for循环

            最先执行初始化,可以声明一种类型,可以初始化一个或多个变量,也可以是空语句
            然后检测布尔表达的值,如果是true,执行循环的语句,如果是false,终止循环
            执行一次后,迭代,即为更新变量的值
            再接着检测布尔值,再循环
            */
            
        }
//        for (;;);
//        死循环
    }
}
    

计算0-100之间奇数和偶数的和

public class ForDemo02 {
    public static void main(String[] args) {
//        计算0-100之间奇数和偶数的和
        int oddsum =0;
        int evensum =0;
        for (int i = 0; i <= 100; i++) {
            if (i%2==0){
                oddsum+=i;//偶数
            }else evensum+=i;//奇数


        }
        System.out.println("偶数和"+oddsum);
        System.out.println("奇数和"+evensum);

    }
}

用for循环输出1000以内5的倍数,每行输出3个

public class ForDemo03 {
    public static void main(String[] args) {
//        用for循环输出1000以内5的倍数,每行输出3个
        for (int i = 0; i < 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
//                System.out.print("\n");
//                换行
//                println输出完会换行
//                print输出完不会换行

            }

        }

    }
}

打印99乘法表

public class ForDemo04 {
    public static void main(String[] args) {
//        99乘法表

        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i+"*"+j+"="+(j*i)+"\t");



        }
            System.out.println();


        }

    }
}

增强for循环

Java5引入了一种主要用于数组或集合的增强型for循环。

public class ForDemo05 {
    public static void main(String[] args) {
       int[] number ={10,20,30,40,50};
       for (int x:number) {
           System.out.println(x);
//           遍历数组中的元素
       }

    }
}

学数组会具体讲

posted @ 2021-02-17 17:26  Rookik  阅读(97)  评论(0)    收藏  举报