首页 > 其他分享 >Day9:学习循环结构

Day9:学习循环结构

时间:2023-01-04 22:57:25浏览次数:42  
标签:struct Day9 int void 学习 while 循环 public

循环结构

  • while 循环
  • do…while 循环
  • for 循环
  • 在java5中引入了一种主要用于数组的增强型for循环

while 循环

  • while是最基本的循环,它的结构为:

    while(布尔表达式){
        //循环内容
    }
    
  • 只要布尔表达式为true , 循环就会一直执行下去。

  • 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。

    package com.dfyfhqsgclxry.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.dfyfhqsgclxry.struct;
    
    public class WhileDemo02 {
        public static void main(String[] args) {
            //死循环
            while (true){
                //等待客户端链接
                //定时检查
                //。。。。。。。。。
            }
        }
    }
    
  • 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环,会影响程序性能或者造成程序卡死崩溃!

  • 思考题:计算1+2+3+……+100=?

    package com.dfyfhqsgclxry.struct;
    
    public class WhileDemo03 {
        public static void main(String[] args) {
            int i = 1;
            int sum = 0;
            while (i<=100){
               sum  = sum + i;
               i++;
            }
            System.out.println(sum);
        }
    }
    

do…while 循环

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

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

    do{
        //代码语句
    }while(布尔表达式);
    
  • while 和 do…while 的区别:

    • while 先判断后执行。 do…while是先执行后判断!

      package com.dfyfhqsgclxry.struct;
      
      public class DoWhileDemo01 {
          public static void main(String[] args) {
              int i = 1;
              int sum = 0;
              do {sum  = sum + i;
                  i++;
              } while (i<=100);
              System.out.println(sum);
          }
      }
      
    • do…while 总是保证循环体会被至少执行一次!这是他们的主要差别。

      package com.dfyfhqsgclxry.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 循环

  • 虽然所有循环结构都可以用while或者do…while表示,但java提供了另一种语句——for循环,使一些循环结构变得更加简单。

  • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。

  • for循环执行的次数是在执行前就确定的。语法格式如下:(快捷键:100.for

    for(初始化;布尔表达式;更新){
        //代码语句
    }
    
    package com.dfyfhqsgclxry.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 (int i=1;i<=100;i+=2){
                System.out.println(i);
            }
            System.out.println("for循环结束!");
            /*
            关于for循环的几点说明:
            最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
            然后检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
            执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
            再次检测布尔表达式,循环执行上面的过程。
             */
        }
    }
    
  • 练习1:计算0到100之间的奇数和偶数和

    package com.dfyfhqsgclxry.struct;
    
    public class ForDemo02 {
        public static void main(String[] args) {
            //练习1:计算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);
        }
    }
    
  • 练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

    package com.dfyfhqsgclxry.struct;
    
    public class ForDemo03 {
        public static void main(String[] args) {
            //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
            for (int i = 1; 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 输出完不会换行
            System.out.println("===========================");
    
            int a = 1;
            while (a<=1000){
                a++;
                if (a%5==0){
                    System.out.print(a+"\t");
                }
                if (a%(5*3)==0){
                    System.out.println();
                }
            }
        }
    }
    
  • 练习3:打印九九乘法表

    package com.dfyfhqsgclxry.struct;
    
    public class ForDemo04 {
        public static void main(String[] args) {
            //1.先打印第一列
            //2.把重复的1再用一个循环包起来
            //3.去掉重复项,i <= j
            //4.调整样式
            for (int j = 0; j <= 9; j++) {
                for (int i = 1; i <= j ; i++) {
                    System.out.print(i+"*"+j+"="+(i*j)+"\t");
                }
                System.out.println();
            }
        }
    }
    

增强for循环

  • 这里我们只是先见一面,做个了解,之后数组我们重点使用

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

  • Java增强for循环语法格式如下:

    for(声明语句:表达式)
    {
        //代码句子
    }
    
  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

  • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

    package com.dfyfhqsgclxry.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

  • break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)

    package com.dfyfhqsgclxry.struct;
    
    public class BreakDemo {
        public static void main(String[] args) {
            int i = 0;
            while (i<100){
                i++;
                System.out.println(i);
                if (i==30){
                    break;
                }
            }
            System.out.println("程序运行完成");
        }
    }
    
  • continue语句在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

    package com.dfyfhqsgclxry.struct;
    
    public class ContinueDemo {
        public static void main(String[] args) {
            int i = 0;
            while (i<100){
                i++;
                if (i%5==0){
                    System.out.println();
                    continue;
                }
                System.out.print(i);
            }
        }
    }
    
  • 关于goto关键字

    • goto关键字很早就在程序设计语言中出现。尽管goto仍是java的一个保留字,但并未在语言中得到正式使用;java没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子---带标签的break和continue
    • “标签”是指后面跟一个冒号的标识符,例如:label:
    • 对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,他们就会中断到存在标签的地方。
    package com.dfyfhqsgclxry.struct;
    
    public class LabelDemo {
        public static void main(String[] args) {
            //打印101-150之间的所有质数
            //质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。
            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+" ");
            }
        }
    }
    

练习:打印三角形

package com.dfyfhqsgclxry.struct;

public class TextDemo {
    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();
        }
    }
}


标签:struct,Day9,int,void,学习,while,循环,public
From: https://www.cnblogs.com/dfyfhqsgclxry/p/17026217.html

相关文章

  • Python Kconfiglib初次学习
    1参考kconfiglib库官方介绍:kconfiglib·PyPIKconfiglib源码:GitHub-ulfalizer/Kconfiglib:AflexiblePython2/3KconfigimplementationandlibraryKconfig语法......
  • 20230103_每日学习记录
    20230103做多线程爬虫,需要有些对抗反扒机制的措施.有些时候直接写多线程,比如python的multiprocessing,会发现抓不下来东西.这也可能是我的爬虫没写好.但是就是发现同......
  • “山外有山比山高”-为什么叫深度学习?​
    1.模型的复杂表示​在上一节中(​​"众里寻他千百度"-深度学习的本质​​),讨论了如何通过简单的回归模型预测未来一天的youtube频道的观看人数。事实上,在上一节的案例介绍中......
  • 最小表示法学习笔记
    假设我们有一个字符串\(s\),下标从\(1\)到\(n\),我们将字符串复制一遍接在尾部,设新的字符串为\(ss\),对于\(1\leqi\leqn\)显然有\(ss_i=ss_{i+n}\)。对于\(1\leq......
  • Markdown学习笔记——DAY01
    Markdown学习标题:#+空格学习二级标题:##+空格以此类推字体粗体helloworld两星斜体helloworld一星粗体加斜体helloworld三星划去线helloworld两波浪引用......
  • mybaits 笔记2022年8月学习笔记
    mybatis整理前期准备安装必要依赖:idea开发mybatis,如果学习测试,可以在一个直接建一个空白项目,如果是用springboot,则建议用用boot的安装捆绑方式核心依赖org.mybatis......
  • MAUI Blazor学习4-绘制BootstrapBlazor.Chart图表
    MAUIBlazor学习4-绘制BootstrapBlazor.Chart图表 MAUIBlazor系列目录MAUIBlazor学习1-移动客户端Shell布局-SunnyTrudeau-博客园(cnblogs.com)MAUIBlazor学......
  • [12]机器学习_smote算法
    1、smote原理介绍在两个点连线中间取点2、smote算法实现importrandomfromsklearn.neighborsimportNearestNeighborsimportnumpyasnpimportmatplotlib.pyplotasplt......
  • WSL配置wslg与深度学习环境
    下载直接去MicrosoftStore下就可以了,安装默认在C盘,要修改的话可以改成wsl--exportUbuntu-22.04e:\ubuntu22.04.tarwsl--unregisterUbuntu-22.04wsl--importUbu......
  • FreeSWITCH学习笔记3(3.5)- 初识FreeSWITCH
    目录配置SIP网关拨打外部电话:从某一分机上呼出呼入电话处理 配置SIP网关拨打外部电话 originatesofia/gateway/zlz/1003&echo(前提是设置了1003,并且选定了才......