黑马程序员_程序流程控制

来源:互联网 发布:java信贷项目 编辑:程序博客网 时间:2024/05/16 08:52

------- android培训、java培训、期待与您交流! ----------

 

 

 

程序流程控制有四种类型:

        

        一、顺序结构;

        二、判断结构;

        三、选择结构;

        四、循环结构。

 

        顺序结构比较简单,下面从判断结构、选择结构来介绍:

        

         一、判断结构使用的是 if   语句   if语句有三种格式:

        

        1、  if(条件表达式){                                                            2、if(条件表达式){                                                                     3、if(){

                            执行语句;                                                                                  执行语句;                                                                         执行语句;            

                        }                                                                                                    }else{                                                                                   }else  if(条件表达式){

                                                                                                                                    执行语句;                                                                         执行语句;

                                                                                                                                   }                                                                                          }

                                                                                                                                                                                                                                 ......

                                                                                                                                                                                                                        }else{

                                                                                                                                                                                                                                   执行语句;

                                                                                                                                                                                                                          }

 

       2、题目:根据用户输入的月份,打印出对应的季节;写代码时的思路很重要,有了思想,理清思路,代码可以很顺利的写出来:

     

    if    语句                                                                                                                                                        

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

        int x=3;                                                                                                                                          

        if(x>12  ||  x<1){                                                                                                                      
        System.out.println("no");                                                                                                        
     }                                                                                                                                                     
     else if(x>=3 && x<=5){                                                                                                                  
      System.out.println("春季");                                                                                                      
     }                                                                                                                                                     
     else if(x>=6 && x<=8){                                                                                                                  
      System.out.println("夏季");                                                                                                        
     }                                                                                                                                                        
     else if(x>=9 && x<=11){                                                                                                                
      System.out.println("秋季");                                                                                                         
     }                                                                                                                                                       
     else{                                                                                                                                                                                                                                                                                       
     System.out.println("冬季");
      }         
   }
}

    if  语句相当于判断题                                                                                                                                               

同一个题目用switch语句的写法

class Demo
{
  public static void main(String[] args){
       int x=8;
    switch(x){
     case 3:
     case 4:
     case 5:
            System.out.println("春季");
          break;
     case 6:
     case 7:
     case 8:
            System.out.println("夏季");
          break;
     case 9:
     case 10:
     case 11:
            System.out.println("秋季");
          break;
     case 12:
     case 1:
     case 2:
            System.out.println("冬季");
          break;
   default:
               System.out.println("非法");
          break;

    }
  }
}

switch 语句相当于选择题

 

二、if   switch  什么时候使用?

 

     答:  1、判断的具体数值不多,且符合byte、char、short、int  这四种基本数据类型,建议使用switch语句;

    

              2、于区间的判断,结果为Boolean类型的判断,使用  if    语句。

         

 

 

三、循环结构的种类有三种:

         

          1、while语句;                                                                                    2、do... while语句                                                                                    3、for语句

 

         (1)、 while的格式:

                       while(条件表达式){

                                      执行语句;

                             }

 

          (2)、do...while 的格式:

                     do{

                                 执行语句;

                          }while(条件表达式);

 

            (3)、for的格式

                

                 1、  for(初始化表达式;条件表达式;循环后操作的表达式){

                             执行语句;

                      }

                

                  2、for循环结构执行的循序:  (1)、先初始化表达式执行一次 

                                                                        (2)、再条件表达式执行,满足条件执行循环体,

                                                                         (3)循环体执行结束,执行循环后的操作表达式,

                                                                         (4)执行循环后操作表达式完成后,再执行条件表达式,满足条件后,执行循环体,

                                                                          (5)按照此顺序重复执行,直到不满足条件为止,退出程序;

   

 

    记住:

    1、语句结束的两个方式:     1、}结束

                                                        2、;结束

  

    2、  while循环和for循环可以互换;

 

 

四、嵌套循环 (以for循环为例打印等腰三角形)

————*

———*      *

——*       *     *

—*      *        *     *

*      *     *        *    *

思想:1、先打印一个图中—围成的三角形;

            2、在打印*和空格;

 

 

    对应的代码:

 

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

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

            }

 

这是三个for循环结构使用打印出等腰三角形的应用。

 

 

五、break  、和continue的使用?

 

 答、   1、break  只能用于switch  和循环结构中,continue只能用于循环结构中;

   

           2、break、continue  作为单独语句是不能使用的,因为程序中break、和continue下面的语句都执行不到,显示为无法访问的错误。

 

 

0 0