循环结构

循环结构


while循环

while(){

}

package com.yang.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1-100
        int i = 0;
        while(i < 100){
            i++;
            System.out.println(i);
        }
    }
}

package com.yang.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环 避免死循环
        while (true){
            //等待客户端连接
            //定时检查
            //....

        }
    }
}

package com.yang.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+。。。+100=?

        //高斯的故事
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}

Do while循环


do{

}while();

package com.yang.struct;

public class WhileDemo04 {
    public static void main(String[] args) {
        //用while或for循环输出1-1000之间能被5整除的数,并每行输出3个
        int i = 0;
        int sum = 0;
        while(i <= 1000 ){
            i++;
            if (i%5==0){
                System.out.print(i+"\t");
                sum++;
            }
            if(sum%3==0){
                //System.out.print("\t");
                System.out.println();
            }
        }


    }
}
package com.yang.struct;

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);
    }
}

package com.yang.struct;

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

for循环


For(初始化;布尔表达式;更新){

代码语句

}

package com.yang.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1;//初始条件

        while (a<=100){//循环条件
            System.out.println(a);//循环体
            a+=2;//迭代
        }
        System.out.println("while循环结束!");

        //for最有效,最灵活的循环语句
        for (int i = 1;i <= 100;i++){//初始化 条件 跌倒
            System.out.println(i);
        }

        System.out.println("循环结束");

        /*
    关于for循环的说明
    最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
    然后检测布尔表达式的值。如果为true,循环体被执行。如果是false,循环终止,开始执行循环体后面的语句。
    执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
    再次检测布尔表达式。循环执行上面的过程。
     */
      //死循环
        for (; ; ) {

        }
    }


}

package com.yang.struct;

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;//oddSum = oddSum + i;
            }else {//偶数
                evenSum+=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);
    }
}

package com.yang.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1-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("\t");
            }

            //println 输出完换行
            //print  输出完不换行
        }
    }
}

package com.yang.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        //九九乘法表
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    }
}

增加for循环

重点形容数组和集合

package com.yang.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义一个数组

        for(int i = 0;i < 5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("=============");
        //遍历数组的元素
        for(int x: numbers){
            System.out.println(x);
        }
    }
}

break和continue和goto

break

用与强行退出循环

package com.yang.struct;

public class BreakDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
        System.out.println("123");
    }
}

continue

用与终止某次循环尚未执行的部分

package com.yang.struct;

public class ContinueDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            if(i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
        //break在任何循环语句主体部分,均可用break控制循环过程
        //break用与强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
        //continue语句用户在循环语句中,用与终止某次循环中尚未执行的语句,接着进行下一次是否执行循环的判断。
        //一个跳出循环结构 一个跳过本次循环
    }
}

goto

package com.yang.struct;

public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间所有质数
        //不建议使用
        int count = 0;
        outer:for (int i = 101;i<150;i++ ){
            for (int j = 2;j<i/2;j++){
                if (i%j==0){
                   continue outer;
                }
            }
            System.out.print(i+" ");
        }
    }
}

text

package com.yang.struct;

public class Text01 {
    public static void main(String[] args) {
        //打印三角形 5行

        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i ; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }
}

posted @ 2024-08-15 18:21  yoyy3  阅读(18)  评论(0)    收藏  举报