Java/JSP/Statements — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:10, 1 июня 2010

For loop: Using Two Loop Indexes

<HTML>
  <HEAD>
    <TITLE>Using Two Loop Indexes</TITLE>
  </HEAD>
  <BODY>
    <H1>Using Two Loop Indexes</H1>
    <%
        for (int i = 0, doubleIndex = 0; i <= 10;
            i++, doubleIndex = 2 * i) {
            out.println("i: " + i +
                " doubleIndex: " + doubleIndex + "<BR>");
        }
    %>
  </BODY>
</HTML>





Nested if Statements

<HTML>
  <HEAD>
    <TITLE>Nested if Statements</TITLE>
  </HEAD>
  <BODY>
    <H1>Nested if Statements</H1>
    <%
        double value = 2;
        if (value != 0) {
            if (value > 0)
               out.println("The result = " + (1 / value));
            else
               out.println("Sorry, we need a positive number.");
        }
    %>
  </BODY>
</HTML>





Testing for Multiple Conditions

<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:
                out.println("Nice!");
                break;
            case 70:
            case 74:
            case 75:
                out.println("Fairly warm.");
                break;
            default:
                out.println("Too hot!");
        }
    %>
  </BODY>
</HTML>





Using an if-else Ladder

<HTML>
  <HEAD>
    <TITLE>Using an if-else Ladder</TITLE>
  </HEAD>
  <BODY>
    <H1>Using an if-else Ladder</H1>
    <%
        String day = "Friday";
        if(day == "Monday")
            out.println("It\"s Monday.");
        else if (day == "Tuesday")
            out.println("It\"s Tuesday.");
        else if (day == "Wednesday")
            out.println("It\"s Wednesday.");
        else if (day == "Thurssday")
            out.println("It\"s Thursday.");
        else if (day == "Friday")
            out.println("It\"s Friday.");
        else if (day == "Saturday")
            out.println("It\"s Saturday.");
        else if (day == "Sunday")
            out.println("It\"s Sunday.");
    %>
  </BODY>
</HTML>





Using Compound Statements

<HTML>
  <HEAD>
    <TITLE>Using Compound Statements</TITLE>
  </HEAD>
  <BODY>
    <H1>Using Compound Statements</H1>
    <%
        int value = 10;
        if(value > 0) {
            out.println("The number was positive.");                
            out.println("Absolute value of " + value + " = " + value);
        }
    %>
  </BODY>
</HTML>





Using the break Statement

<HTML>
  <HEAD>
    <TITLE>Using the break Statement</TITLE>
  </HEAD>
  <BODY>
    <H1>Using the break Statement</H1>
    <%
        double array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int sum = 0;
        for(int i = 0; i <
            array.length; i++) {
            sum += array[i];
            if (sum > 12) break;
            out.println("Looping...<BR>");
        }
        out.println("The sum exceeded the maximum allowed value.");
    %>
  </BODY>
</HTML>





Using the continue Statement

<HTML>
  <HEAD>
    <TITLE>Using the continue Statement</TITLE>
  </HEAD>
  <BODY>
    <H1>Using the continue Statement</H1>
    <%
        for(double i = 6; i >= -6; i--) {
            if (i == 0) continue;
            out.println("The reciprocal of " + i +
                " is " + (1 / i) + ".<BR>");
        }
    %>
  </BODY>
</HTML>





Using the if Statement

<HTML>
  <HEAD>
    <TITLE>Using the if Statement</TITLE>
  </HEAD>
  <BODY>
    <H1>Using the if Statement</H1>
    <%
        int i;
        for (i = 1; i <= 10; i++) {
            out.println("This is iteration number "
                + i + "<BR>");
        }
    %>
  </BODY>
</HTML>





Using the switch Statement

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





Using the while Loop

<HTML>
  <HEAD>
    <TITLE>Using the while Loop</TITLE>
  </HEAD>
  <BODY>
    <H1>Using the while Loop</H1>
    <%
        int value = 5;
        while (value > 0) {
            out.println("The value is now " + value-- + ".<BR>");
        }
    %>
  </BODY>
</HTML>





While: Finding a Factorial

<HTML>
  <HEAD>
    <TITLE>Finding a Factorial</TITLE>
  </HEAD>
  <BODY>
    <H1>Finding a Factorial</H1>
    <%
        int value = 6, factorial = 1, temporaryValue = value;
        while (temporaryValue > 0) {
            factorial *= temporaryValue;
            temporaryValue--;
        }
        out.println("The factorial of " + value + " is " + factorial + ".");
    %>
  </BODY>
</HTML>





While statement

<HTML>
  <HEAD>
    <TITLE>Finding Reciprocals</TITLE>
  </HEAD>
  <BODY>
    <H1>Finding Reciprocals</H1>
    <%
        double values[] = {4, 3, 2, 1, 0, 1, 2, 3, 4};
        int i = 0;
        while (values[i] != 0){
            out.println("The reciprocal = " + 1 / values[i++] + ".<BR>");
        } 
    %>
  </BODY>
</HTML>