工作小驛

          Ninja!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            103 Posts :: 0 Stories :: 36 Comments :: 0 Trackbacks

              這節課我們又要講語法了,這是6節課,我們這個教程側重的是實踐的內容和語言的重點。在java語言中還有很多細節的東西,請參考sun公司的官方培訓教程。我們這里不能一一講述。這節課我們來給大家提供一些程序流程控制的一些例子供大家學習。計算機怎么做事情,是我們教給他的。我們用它解決實際生活中的問題,所以計算機要描述現實生活中的流程。Java語言中提供了4類程序控制語句,來描述流程:

                         1.循環語句:while,do-while,for

          2.分支語句:if-else,switch,

          3.跳轉語句 break,continue,label: return

          4.異常處理語句:try-catch-finally,throw

          實踐:

              1.循環語句

                        while 語句

          class While {

          public static void main(String args[]) {

          int n = 10;

          while(n > 0) {                                   

          System.out.println("tick " + n);

          n--;

          }

          }

          }

                        do…while 語句

                        class DoWhile {

          public static void main(String args[]) {

          int n = 10;

          do {

          System.out.println("tick " + n);

          n--;

          } while(n > 0);

          }

          }

          二者區別,do…while至少循環一次,而while的表達式要是為flase的話可以一次也不循環。再通俗一點,do…while就算是括號里的是flase,人家最少也能do一次。

                  for語句

                        class ForTick {

          public static void main(String args[]) {

          int n;

          for(n=10; n>0; n--)

          System.out.println("tick " + n);

          }

          }

          與上面那兩個的區別,for循環執行的次數是可以在執行之前確定的。通俗一點說吧,看這個例子 for(n=10; n>0; n--)就是在括號里的時候,就已經知道要循環10次了。

          還有啊,for循環的部分可以為空的

          class ForVar {

          public static void main(String args[]) {

          int i;

          boolean done = false;

          i = 0;

          for( ; !done; ) {

          System.out.println("i is " + i);

          if(i == 10) done = true;

          i++;

          }

          }

          }   循環語句的例子下載

              2.分支語句 

                               if/else語句

          class IfElse {

          public static void main(String args[]) {

          int month = 4; // April

          String season;

          if(month == 12 || month == 1 || month == 2)

          season = "Winter";

          else if(month == 3 || month == 4 || month == 5)

          season = "Spring";

          else if(month == 6 || month == 7 || month == 8)

          season = "Summer";

          else if(month == 9 || month == 10 || month == 11)

          season = "Autumn";

          else

          season = "Bogus Month";

          System.out.println("April is in the " + season + ".");

          }

          }

          //這段程序輸出:

          //April is in the Spring.

          // 注意 ||”是或運算

                               switch語句

                               class Switch {

          public static void main(String args[]) {

          int month = 4;

          String season;

          switch (month) {

          case 12:

          case 1:

          case 2:

          season = "Winter";

          break;

          case 3:

          case 4:

          case 5:

          season = "Spring";

          break;

          case 6:

          case 7:

          case 8:

          season = "Summer";

          break;

          case 9:

          case 10:

          case 11:

          season = "Autumn";

          break;

          default:

          season = "Bogus Month";

          }

          System.out.println("April is in the " + season + ".");

          }

          分支語句代碼下載

          switch語句適合于條件非常多的邏輯

          請看上述語句可以混合使用,請看下載例子

           

          循環語句的例子下載

          分支語句代碼下載

          綜合使用的代碼下載

          如有問題請登陸論壇

          posted on 2007-07-16 09:55 王君 閱讀(246) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 府谷县| 横峰县| 新昌县| 柘城县| 类乌齐县| 康平县| 天津市| 哈尔滨市| 临沭县| 邵武市| 汕头市| 孙吴县| 龙南县| 紫云| 江安县| 徐州市| 松江区| 独山县| 湖州市| 轮台县| 淮南市| 英德市| 武义县| 南岸区| 新巴尔虎左旗| 荣昌县| 库伦旗| 桃源县| 肃宁县| 平乡县| 济阳县| 福安市| 汝城县| 邵阳县| 安吉县| 历史| 苍梧县| 孝昌县| 新密市| 嘉兴市| 屏东市|