jsp switch語句的用法

來源:互聯網
上載者:User

如果希望選擇執行若干代碼塊中的一個,你可以使用switch語句:

文法:
switch(n)
   {
   case 1:
     執行代碼塊 1
     break
   case 2:
     執行代碼塊 2
     break
   default:
     如果n即不是1也不是2,則執行此代碼
   }
工作原理:switch後面的(n)可以是運算式,也可以(並通常)是變數。然後運算式中的值會與case中的數字作比較,如果與某個case相匹配,那麼其後的代碼就會被執行。break的作用是防止代碼自動執行到下一行。

<HTML>
  <HEAD>
    <TITLE>Using the switch Statement</TITLE>
  </HEAD>

  <BODY>
    <H1>Using the switch Statement</H1>
    <%
        int day = 3;

        switch(day) {
            case 0:
                out.println("It's Sunday.");
                break;
            case 1:
                out.println("It's Monday.");
                break;
            case 2:
                out.println("It's Tuesday.");
                break;
            case 3:
                out.println("It's Wednesday.");
                break;
            case 4:
                out.println("It's Thursday.");
                break;
            case 5:
                out.println("It's Friday.");
                break;
            default:
                out.println("It must be Saturday.");
        }
    %>
  </BODY>
</HTML>


另一種情況

<HTML>
  <HEAD>
    <TITLE>Testing for Multiple Conditions</TITLE>
  </HEAD>

  <BODY>
    <H1>Testing for Multiple Conditions</H1>
    <%
        int temperature = 64;

        switch(temperature) {
            case 60:
            case 61:
            case 62:
                out.println("Sorry, too cold!");
                break;
            case 63:
            case 64:
            case 65:
                out.println("Pretty cool.");
                break;
            case 66:
            case 67:
            case 68:
            case 69:
                out.println("Nice!");
                break;
            case 70:
            case 71:
            case 72:
            case 73:
            case 74:
            case 75:
                out.println("Fairly warm.");
                break;
            default:
                out.println("Too hot!");
        }
    %>
  </BODY>
</HTML>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.