【JavaSE】07-循环结构[while,do···while,for]

①.while循环

结构
while(布尔表达式){
//循环语句
}
只要布尔表达式为true那循环就会一直进行下去。
通常是需要一个能让循环停下来的方法的,不能让程序一直循环。
少部分循环会一直执行,例如服务器请求的响应监听。

//使用while循环实现1+2+3···+100
    public static void main(String[] args){
        int i = 0;
        int sum = 0;
        while(i <= 100){
            sum += i;
            i++;
            System.out.println(sum);
        }

②.do···while循环

与while区别:即使不满足条件,也能保证程序执行了一次。

public static void main(String[] args){
        int a = 0;

        //条件不满足while语句不执行
        while(a < 0){
            System.out.println(a);  //不输出
            a++;  //不执行
        }
        System.out.println("=====================");
        //即使条件不满足,也先执行一遍do之后的语句
        do{
            System.out.println(a);  //输出0
            a++;
        }while(a < 0);
        
        //使用do···while实现1+2+3···+100
        int sum = 0;
        do{
            sum += a;
            a++;
            System.out.println(sum);
        }while(a <= 100);
    }

③.for循环

  • for循环是支持迭代的一种通用结构,是最有效,最灵活的通用结构。
  • 语法格式:
    for(初始化表达式;布尔表达式;更新表达式){
    //循环代码语句
    }
  • for循环的循环次数是在定义for语句时就限定的。
    在IDEA中快捷键 100.for+Enter,可生成 for (int i = 0; i < 100; i++)

for循环当中三个式子都是可以省略的

//计算输出0~100的奇数与偶数的和
    public static void main(String[] args){
        int oddsum = 0;
        int evensum = 0;

        for (int i = 0; i <= 100; i++) {
            if(i % 2 == 0){
                evensum += i;
            }else{
                oddsum += i;
            }
        }
        System.out.println("oddsum = " + oddsum);
        System.out.println("evensum = " + evensum);
    }
public static void main(String[] args){
        //实现1000以内能够被5整除的数的输出,并做到内行输出6个
        //while循环实现
        int w = 1;
        while(w <= 1000){
            if(w % 5 == 0) System.out.print(w + "\t");   //判断是否能够被5整除
            if(w % (5*6) == 0) System.out.println();     //计数满6个输出换行符
            w++;
        }
        System.out.println("\n==============================================");

        //for循环实现
        for (int i = 1; i <= 1000; i++) {
            if(i % 5 == 0) System.out.print(i + "\t");  //判断是否能够被5整除
            if(i % (5*6) == 0) System.out.println();    //计数满6个输出换行符
        }
    }

④.增强for循环

  • 作用:JDK5之后引入的新特性,主要用来遍历数组集合的。
  • 语法:
    for(声明语句表达式){
    //代码语句
    }
  • 注意
    声明语句:声明的局部变量的类型必须和数组元素的类型匹配。它的值在循环中和对应数组元素的额值相等。
    表达式:它是需要访问遍历的数组名,或者是返回值为数组的方法。
public static void main(String[] args){
        int[] numbers = {10,20,30,40,50};   //定义一个数组并存储一些数据

        for(int x:numbers){   //该语句会逐个查询numbers数组当中的数,并且赋值给x
            System.out.println(x);
        }

        System.out.println("====================================");
        //效果等同于
        for(int x = 0;x < 5;x++){
            System.out.println(numbers[x]);
        }
    }

⑤.break与continue

break:在任何循环语句的主体部分,均可以用break来控制循环的流程。break用于强制退出循环。
continue:语句在循环体当中,用于终止某一次循环过程。即跳过未执行的语句,重新开始下一轮循环。

public static void main(String[] args){
        int i = 0;
        while(i <= 100){
            i++;
            System.out.println(i);
            if (i == 30) break;    //在i=30 的时候循环就直接被跳出结束了
        }
    }
public static void main(String[] args){
        int i = 0;
        while(i <= 100){
            i++;
            if(i % 10 ==0){           //i能被10整除时跳出循环,直接执行下一条循环
                System.out.println();   
                continue;
            }
            System.out.print("\t" + i);
        }
        /*
            输出
            1	2	3	4	5	6	7	8	9
	        11	12	13	14	15	16	17	18	19
	        21	22	23	24	25	26	27	28	29
            31	32	33	34	35	36	37	38	39
            41	42	43	44	45	46	47	48	49
            51	52	53	54	55	56	57	58	59
            61	62	63	64	65	66	67	68	69
            71	72	73	74	75	76	77	78	79
            81	82	83	84	85	86	87	88	89
            91	92	93	94	95	96	97	98	99
         */
    }

goto语句

早期Java语言的设计是有goto语句的设计的,是通过标签label:来实现语句的跳转的。但是在Java的正式使用中是不被使用的。

⑥打印9*9乘法表,三角形

打印9*9乘法表

public static void main(String[] args){
        //打印9*9乘法表
        /*
            1*1
            1*2 2*2
            1*3 2*3 3*3
            1*4 2*4 3*4 4*4
         */

        for(int x = 1;x <= 9;x++){ //定义x为行号,一共输出9行
            for(int y = 1;y <= x;y++){ //定义列号,当列中的式子输出到小于等于行号时终止循环
                System.out.print("\t" + y + "*" + x + "=" + (y*x));
            }
            System.out.println();
        }
        /*
            1*4 2*4 3*4 4*4
            1*3 2*3 3*3
            1*2 2*2
            1*1
         */
        for(int x = 9;x >= 0;x--){ //更改行数据即可
            for(int y = 1;y <= x;y++){ //定义列号,当列中的式子输出到小于等于行号时终止循环
                System.out.print("\t" + y + "*" + x + "=" + (y*x));
            }
            System.out.println();
        }
            /*
            1*1 1*2 1*3 1*4
            1*2 2*2 2*3 2*4
            1*3 2*3 3*3 3*4
            1*4 2*4 3*4 4*4
         */
        for(int x = 1;x <= 9;x++){ //定义x为行号,一共输出9行
            for(int y = 1;y <= 9;y++){ //更改列数据即可
                System.out.print("\t" + x + "*" + y + "=" + (y*x));
            }
            System.out.println();
        }
    }

打印三角形

public static void main(String[] args){
        int line = 100;
        //尝试打印三角形
        for (int i = 1; i <= line; i++) {
            for(int j = i;j <= line;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();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值