(十)Core Java 语句 (92)

 
目录 :          
1 ) .语句( if ) 

2 ) . 语句( if练习 ) 

3 ) . 语句( switch ) 

4 ) .  语句(switch练习 ) 

5 ) .  语句( while ) 

6 ) .  语句( do while ) 

7 ) .   语句( for ) 

8 ) .  语句(for 和while的区别之处  ) 

9 ).   语句( 循环语句的其他特点 ) 

10 ). 语句( for 嵌套 ) 

11 ). 语句( for 嵌套练习 ) 

12 ). 语句( for嵌套-九九乘法表 ) 

13 ). 语句( break-continue ) 

14 ) . 语句( 练习 ) 


     一 .   语句( if ) 

1 ) . 程序流程控制方式 的四大结构  --> 主要指if语句

1.1 顺序结构  --> 从上至下顺序执行的方式

1.2  判断结构 --> if  else  if  else  

1.3  选择结构--> switch(){case : { break;} case:{break;} default : {} }

1.4  循环结构--> while ,dowhile , for

2 ) . if语句的三种格式 :


2.1 if(条件表达式) {执行语句;}    --> 一层判断

2.2 if(条件表达式){执行语句;}else{执行语句;}     --> 两层判断

2.3 if(条件表达式){执行语句;}else if(条件表达式){执行语句} ...else{执行语句;}     --> 多层层判断




3 ) .  if else  与三元运算符的区别 :   在于三元运算符必须要有一个结果

3.1  if esle 的简写格式 :   变量 =(条件表达式)?表达式1 ;表达式2 ;

3.2 三元运算符 : 

好处 : 可以简化if else 代码

弊端 : 因为是一个运算符,所以运算完必须要有一个结果

例如 :  int b ,a =5;     三元运算方式:      b=(a>1)?100:200 ;
 
 4 ) . 


小结 :  

           1.  当{}去掉的时候 if仅控制离它最近的一条语句
        
           2.  三元运算符是if else 的简写格式

           3. if else 的区间中中只要有一个满足则结束判断

           4. 理解代码区块的含义
 

       二.  语句( if练习 ) 


1 ) . 需求 一 :  根据用户定义的数值的不同,打印对应的星期英文

class day04
{
    public static void main(String[] args)
    {
        
        //需求1 : 根据用户定义的数值的不同,打印对应的星期英文
        
        int num=1;
        
        if(num==1)
            System.out.println("moonday");
        else if(num==2)
            System.out.println("Tuesday");
        else if(num==3)
            System.out.println("Wednesday");
        else if(num==4)
            System.out.println("Thursday");
        else if(num==5)
            System.out.println("Friday");
        else if(num==6)
            System.out.println("Saturday");
        else if(num==7)
            System.out.println("Sunday");
        else   
            System.out.println("nono");
    
    
    
    }
    
}

2 ) .  需求 二  根据用于指定月份,打印该月所属的季节

class IfTextMonth
{
    public static void main(String[] args)
    {
    
        //需求2:根据用于指定月份,打印该月所属的季节
        //3,4,5 春季   6,7,8 夏季  9,10,11, 秋季  12,1,2 冬季
    
    
        //方式一 :
        
    /*     int month=1;
        
        if(month==3 || month == 4 || month == 5)
            System.out.println(month+"月是春季");
        else if(month==6 || month == 7 || month ==8)
            System.out.println(month+"月是夏季");
        else if(month==9 || month == 10 || month == 11)
            System.out.println(month+"月是秋季");
        else if(month==12 || month == 1 || month == 2)
            System.out.println(month+"月是冬季");
        else
            System.out.println(month+"月是不存在的,年轻人"); */
    


         //方式二 :
        
        int month=1;
        
        if(month>12||month<1)
            System.out.println(month+"月是不存在的,年轻人");
        else if(month==3 || month == 4 || month == 5)
            System.out.println(month+"月是夏季");
        else if(month==6 || month == 7 || month ==8)
            System.out.println(month+"月是夏季");
        else if(month==9 || month == 10 || month == 11)
            System.out.println(month+"月是秋季");
        else  
            System.out.println(month+"月是冬季");
        
        
        
    
    }
    
}

 

小结 :  

           1.  思路一 : 先判断符合标准的,再排除不符合标准的
        
           2.  思路二 : 先排除不符合标准的,再判断剩下符合标准的

    

      三.  . 语句( switch ) 

1 ) . 简单演示使用 :

class SwitchText
{
    public static void main(String[] args)
    {
    
        //简单演示switch
         
    int i=1;
    
     switch(i){
        
        case 1:
            System.out.println("该数值是"+i);
            break;
        case 2:
            System.out.println("该数值是"+i);    
            break;
        case 3:
            System.out.println("该数值是"+i);    
            break;
        case 4:
            System.out.println("该数值是"+i);
            break;
        case 5:
            System.out.println("该数值是"+i);
            break;
        default :
            System.out.println("非法数值");
        
     }
      
    
    }
    
}
 

2 ) . Switch语句的特点 :

2.1 switch语句选择的类型只有四种 : byte,short,int,char 

2.2case之间与default没有顺序,先执行第一个case,没有匹配的case执行default

2.3 结束switch语句的两种情况 : 遇到break ,执行到switch 语句结束

2.4 如果匹配的case或者default没有对应的break ,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束

 

小结 :  

           1.  switch中最后一个默认语句后的break可以省略                                                                                                                                                                                        
 

      四.  语句(switch练习 ) 

1 ) .  需求   根据用于指定月份,打印该月所属的季节

class SwitchText
{
    public static void main(String[] args)
    {
    
        //需求2:根据用于指定月份,打印该月所属的季节
        //3,4,5 春季   6,7,8 夏季  9,10,11, 秋季  12,1,2 冬季
    int month=1;
    
    switch(month){
    
        case 3:
        case 4:
        case 5:
            System.out.println(month+"月是春季");
            break;
            
        
        case 6:
        case 7:
        case 8:
            System.out.println(month+"月是夏季");
            break;
    
        
        case 9:
        case 10:
        case 11:
            System.out.println(month+"月是秋季");
            break;
            
        
        case 12:
        case 1:
        case 2:
            System.out.println(month+"月是冬季");
            break;
            
        default:
            System.out.println(month+"月不存在");
            
    }
    }
    
}

2 ) .   if与switch的区别 : 

3.1  if 优势及常用于 :

[1]  if容易判断区间但 switch不易 ;

[2]  if可以判断布尔类型但if不可以

[3] 因此if常用于判断区间及布尔类型时



3.2 switch优势及常用于 : 

[1] switch的执行效率比if稍高一些,因为break;

[2] switch 在变量符合byte,short,int,char 这四种类型,也就是具体数值不多时做判断

 
 
小结 :  

           1.  switch 判断 数据值少时,if 判断有区间和有布尔类型时
        
 

 五 .   语句( while ) 

1 ) .引入 :  循环结构代表语句  :  while  , do while , for

1.1 三种格式 : 

[1] while (条件表达式){循环体(执行语句);}

[2] do{执行语句;} while(条件表达式);  

[3] for (初始化表达式; 循环条件表达式;循环后的操作表达式){执行语句;}

1.2 须知  :

[1] for里面的表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真就执行循环体,然后再执行循环后的操作表达式,接着继续判断循环条件,重复整个过程,直到条件不满足为止

[2] while与for可以互换,区别在于for为了循环而定义的变量在for循环结束就是在内存中释放,而while循环使用的变量在循环结束后还可继续使用

[3] 最简单的无限循环格式: while(true) ,for(...),无限循环存在的原因是并不知道循环多少次,我们应根据某些条件,来控制循环



 

2 ) . WhileDemo  :

class WhileDemo
{
    public static void main(String[] args)
    {
    
    
     /**
        定义初始化表达式 :
        while(条件表达式){
            循环体(执行语句);
        }
     */
        
         int x=1;
        
         while(x<=10){
            
                 System.out.println("数值"+x);
                
                x++;
         }
        
    }
    
}
 


小结 :  

           1.  有开始就必须有结束
        
    
 

       六.    语句( do while ) 


1 ) . DoWhile Demo : 

class DoWhileDemo
{
    public static void main(String[] args)
    {
    
    
     /**
        定义初始化表达式 :
        do {
            循环体(执行语句);
        }while(条件表达式);
     */
        
         int x=1;
        
        
         do{
            
             System.out.println("Do : 数值"+x);
            
             x++;
            
         }while(x>=10);
        
        
        
    }
    
}


2 ) . while与do while的区别之处 :  

class DoWhileDemo
{
    public static void main(String[] args)
    {
    
    
     /**
        定义初始化表达式 :
        do {
            循环体(执行语句);
        }while(条件表达式);
     */
        
         int x=1;
        
        
         do{
            
             System.out.println("Do : 数值"+x);
            
             x++;
            
         }while(x>=10);
        
        
        
         while(x>=10){
            
              System.out.println("数值"+x);
            
             x++;
         }
        
        
    }
    
}

2.1 以上案例可知,当条件不满足时,while并没有走执行语句,而dowhile 却走了一次执行语句


3 ) .  结论 : 

[1] while  先判断 后执行

[2] do while 先执行一次,而后再判断

 
小结 :  

           1.   dowhile 的特点是 无论条件是否满足,循环体至少被执行一次
        
  

      七.  语句( for ) 

1 ) . ForDemo : 

class ForDemo
{
    public static void main(String[] args)
    {
    
    
     /**
        for(初始化表达式;循环条件表达式;循环后的操作表达式){
            
            执行语句;
        }
        
    
     */
    
    for(int i=1;i<=10;i++){
        
         System.out.println("for数值"+i);
    }
        
    }
    
}

2 ) . 特点 : 

[1] 初始化条件只执行一次

[2] 只要循环条件不满足,则结束


3 ). 格式 :  for (初始化表达式; 循环条件表达式;循环后的操作表达式){执行语句;}

 

小结 :  

           1.  初始化表达式 -->定义变量
        
           2.  循环条件表达式-->做出判断

           3. 循环后的操作表达式-->设置自增

        
 

     八 语句(for 和while的区别之处  ) 

1 ) .    For与while的区别Demo示例 : 

class ForDemo
{
    public static void main(String[] args)
    {
    
    
     /**
        for(初始化表达式;循环条件表达式;循环后的操作表达式){
            
            执行语句;
        }
        
    
        while(条件表达式){
            
            循环体(执行语句);
        }
        
    
     */
    
    for(int i=1;i<=3;i++){
        
         System.out.println("for数值"+i);
    }
    
    
     //System.out.println("for最终数值"+i);

     //for的最终数值是输出不了的,是因为变量i的作用域仅仅在for的区间内
    
      
    int y=1;
    
    while(y<=3){
        
         System.out.println("while数值"+y);
        
         y++;
    }
    
    System.out.println("while最终数值"+y);
        
    }
    
}

2 ) . 区别之处 : 

2.1 变量有自己的作用域,对于for而言,若将用于控制循环的增量定义在for语句中,那该变量只在for语句内有效,for语句执行完毕,该变量在内存中被释放; 而while 则不释放

2.2 for和while可以进行互换,若需要定义循环增量,则for合适;若变量值要多次复用,则while合适


3 ) .  何时使用循环结构?

当要对某些语句执行多次时,就使用循环结构

 
 
小结 :  

           1.  for 循环结束后,for循环体内的变量不占内存,而while中的变量占
        
  
          
 九 .     语句( 循环语句的其他特点 ) 

1 ) . 不要局限自己的思维,觉得for内定是 变量自增,也有可能是其它输出,判断等

  class ForText
{
    public static void main(String[] args)
    {
    
    
     /**
        for(初始化表达式;循环条件表达式;循环后的操作表达式){
            
            执行语句;
        }
        
    
        while(条件表达式){
            
            循环体(执行语句);
        }
        
    
     */
    
    
    int i =1;

    for(System.out.println("A");i<=3;System.out.println("B"),i++){
        
         System.out.println("E");
        
    }
    
    
    
    
}
}



2 ) . 无限循环最简单的的表现形式 : 

2.1 for    -->   for(;;){}

2.2 while  -->  while(true){}


 

小结 :  

           1.  初始化变量值的位置上只能放一个表达式,而循环后的操作表达式可放多个
        
           2.  for循环语句内条件一般情况下必须得有,特殊情况没有条件默认为true : 无限循环

   

       十.  语句( for 嵌套 ) 


1 ) .小练习 : 需求 : 求1-100之间7的倍数有多少个?


class ForText
{
    public static void main(String[] args)
    {

     //步骤 :
    
     //1.定义循环语句,选择for语句
    
     //2.在循环中定义判断,只要是7的倍数即可,使用if语句,条件 : 7的倍数  x%7==0;
    
     //3.定义变量,该变量随着7的倍数的出现而自增
    
    
    
     int count=0;
    
     for(int i=1;i<=100;i++){
        
         if(i%7==0)
             count++;
        
        
     }
    
     System.out.println("在1-100中7的倍数有"+count+"个");
 
}
}


1.1 计算器的思想 : 通过一个变量记录住数据的状态变化 ,通过循环完成

2 ) . 循环嵌套-->直角三角形,尖角朝下

2.1 解释 : 语句嵌套其实也就是语句中还有语句

  
     for(int i=0;i<5;i++){
        
         for(int y=i;y<5;y++){
            
             System.out.print("*");
         }
        
              System.out.println();
        
     }
    
    
    
 


小结 :  

           1.  外循环控制行数,内循环控制列数
        
           2.  记得控制范围即可

      


      十一.  语句( for 嵌套练习 ) 

1 ) .  循环嵌套-->直角三角形,尖角朝上

     for(int i=0;i<5;i++){
        
        
         for(int y=0;y<=i;y++){
            
             System.out.print("*");
         }
        
          System.out.println();
        
     }
    



System.out.println("--------------------------");


      
     for(int i=0;i<=5;i++){
        
         for(int y=1;y<=i;y++){
            
              System.out.print(y);
            
         }
          System.out.println();
     }
    
     


2 ) . 规律 : 

[1] 尖角朝上,可以改变条件 ,让条件随着外循环变化

[2]尖角朝下,可以改变初始值,让初始值随着外循环变化

  

     十二 语句( for嵌套-九九乘法表 ) 

1 ) . 九九乘法表 : 

     for(int x=1;x<=9;x++){
        
         for(int y=1; y<=x; y++){
            
               System.out.print(y+"*"+x+"="+y*x+"\t");
            
         }
           System.out.println();
     }

 

小结 :  

           1.  从宏观的架构层面去理解for循环,切记跳出圈
        
      
          
 十三 . 语句( break-continue ) 

1 ) . 简述 : 

1.1 break (跳出)  --> 应用范围 在 选择结构和循环结构中

1.2 continue(继续) -->应用范围在循环结构

2 ) . 须知 : 

2.1 这两个语句离开应用范围,存在时没有意义的

2.2 这个两个语句单独存在下面都不不可以有语句,因为执行不到

2.3 continue 语句时结束本次循环继续下次循环

2.4 标号的出现,可以让这两个语句作用于指定的范围

3 ) .  演示 : 通过continue的跳出功能来筛选适合的数字,比如 5的倍数

class breakDemo
{
    public static void main(String[] args)
    {
    
     //break
     for(int i=0;i<=5;i++)
     {
        
         for(int y=0;y<=5;y++)        
         {
              System.out.println("*");
              break;
         }
     }
    
    
    
    System.out.println("-----------------------");
    
    int count=0;
    //continue
    for(int i=1;i<=100;i++)
     {
         if(i%5!=0)
             continue;
         System.out.println("五的倍数有:"+i);
         count++;
     }
    
     System.out.println("五的倍数总数是:"+count);
}
}

4 ) . 须知 : 

[1] break 和continue 语句作用的范围

[2] break和continue单独存在时,下面不可以有任何语句,因为都执行不到

[3] break 和continue不可单独存在,因为没有意义


小结 :  

           1.  可以使用标号的方式给循环起名字,是区分循环嵌套结构的一种方式

   2.break是直接跳出当前循环,相当于结束,而continue是跳出当前循环继续循环
 

       十四.  语句( 练习 ) 


1 ) . 等边三角形 : 

class trigon
{
    public static void main(String[] args)
    {
    
     for(int i=0;i<=5;i++){
        
         for(int j=i;j<=5;j++){
            
               System.out.print(" ");
         }

          for(int z=0;z<i;z++){
              
               System.out.print("* ");
              
          }
            System.out.println();
          
          
     }
}
}

2 ) . 效果图 : 


 
 


  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值