当前位置:首页 > 操作系统 > Linux

Linux运维系统工程师与java基础学习系列-5

Java天生骄傲系列-5

 程序流程控制(续):

     选择结构

       switch 语句         

        格式:

          Switch(表达式)

                {

              case 取值 1

                             执行语句;

                             break;

              case 取值 2

                             执行语句;

                             break;

                             ……

                             default:

                   执行语句;

                             break;

                                    }

                牛刀小试1:

                    

        package test.myeclipse;

         public class test1 {

       

               public static void main(String[] args) {

                  int x=3;

                  switch (x)

                 {

                    case 1:

                     System. out .println( ‘1‘ );

                      break ;

                    case 2:

                     System. out .println( ‘2‘ );

                      break ;

                    case 3:

                     System. out .println( ‘3‘ );

                      break ;

                   default :

                     System. out .print( "error" );

                      break ;

       

      

                  }

 

               }

 

}

运行结果: 3


牛刀小试2:   


         package test.myeclipse;

            public class test1 {

              public static void main(String[] args) {

                int x=3;

                switch (x)

               {

                  case 1:

                   System. out .println( ‘1‘ );

                    break ;

                  case 2:

                   System. out .println( ‘2‘ );

                    break ;

                  case 5:

                   System. out .println( ‘3‘ );

                    break ;

                 default :

                   System. out .print( "error" );

                    break ;

       

      

               }

 

              }

 

          }

                运行结果: error


未完待续!








本文出自 “空谷幽兰” 博客,请务必保留此出处http://2489843.blog.51cto.com/2479843/1540371

原文:http://2489843.blog.51cto.com/2479843/1540371


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!