Linux運維繫統工程師與java基礎學習

xupengtao7發表於2014-08-15
程式流程控制(續):

    選擇結構

       switch語句        

        格式:

         Switch(表示式)

                {

              case取值1:

                             執行語句;

                             break;

              case取值2:

                             執行語句;

                             break;

                             ……

                             default:

                  執行語句;

                             break;

                                    }

                牛刀小試1:

                    

        package test.myeclipse;

        publicclass test1 {

       

              publicstaticvoid 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;

           publicclass test1 {

             publicstaticvoid 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


未完待續!


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28904215/viewspace-1251727/,如需轉載,請註明出處,否則將追究法律責任。

相關文章