2021-03-15

Java学习

  • switch多选择结构
    • 多选择结构还有一个实现方式是switch和case语句。
    • switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支
    • switch语句中的变量类型可以是:
      • byte、short、int或者char
      • 从 Java SE 7 开始
      • switch 支持字符串 String 类型
      • 同时 case 标签必须为字符串常量或字面量
package com.cheng.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "程";
        switch (name){
            case "啊":
                System.out.println("不匹配");
                break;
            case "吧":
                System.out.println("不匹配");
                break;
            case "程":
                System.out.println("大帅哥");
                break;
            case "擦":
                System.out.println("不匹配");
                break;
                default:
                    System.out.println("请输入正确字符");
                    break;
        }

    }
}
输出结果:
大帅哥

进程完成,退出码 0
  • 循环结构

    • while循环

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

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

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

      • 少部分情况需要循环一直执行,比如服务器的请求响应监听等。

      • 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!

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

      package com.cheng.struct;
      
      public class WhileDemo03 {
          public static void main(String[] args) {
              int i = 0;
              int sum = 0;
              while (i<=100){
                  sum=sum+i;
                  i++;
              }
              System.out.println(sum);
          }
      }
      输出结果:
          5050
      
      进程完成,退出码 0
      
    • do…while循环

      • 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
      • do…while循环和while循环相似。不同的是,do…while循环至少会执行一次。
      do{
      //代码语句
      }while(布尔表达式);
      
      package com.cheng.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);
              System.out.println(i);
          }
      }
      输出结果:
      5050
      101
      
      进程完成,退出码 0
      
      • while和do…while的区别:
        • while先判断后执行,do…whlie是先执行后判断
        • do…while总是保证循环体会被至少执行一次,这是他们的主要差别
      package com.cheng.struct;
      
      public class DoWhileDemo02 {
          public static void main(String[] args) {
              int a = 0;
              while (a<1){
                  a++;
                  System.out.println(a);
                  do {
                      a++;
                  }while (a<1);
                  System.out.println(a);
              }
          }
      }
      输出结果:
      1
      2
      
      进程完成,退出码 0
      
    • for 循环

      • 虽然所有循环结构都可以用while或者do…while表示,但Java提供了另一种for循环,使一些循环结构变得更加简单。
      • for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
      • for循环执行的次数是在执行前就确定的。其格式如下:
      for(初始化;布尔表达式;更新){
      //代码语句
      }
      
      • 练习一:计算0到100之间的奇数和偶数的和

        package com.cheng.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;
                    }else {
                        evensum+=i;
                    }
                }
                System.out.println("奇数的和为"+oddsum);
                System.out.println("偶数的和为"+evensum);
            }
        }
        
        
      • 练习二:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

        package com.cheng.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("\n");
                    }
                }
            }
        }
        
        
      • 练习三:打印九九乘法表

        package com.cheng.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("\n");
                }
            } 
        }
        输出结果:
        1*1=1	
        
        2*1=2	2*2=4	
        
        3*1=3	3*2=6	3*3=9	
        
        4*1=4	4*2=8	4*3=12	4*4=16	
        
        5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
        
        6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
        
        7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
        
        8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
        
        9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	
        
        
        进程完成,退出码 0
        
        
    • 在Java5中引入了一种主要用于数组的增强型for循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值