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

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

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

Finding Average Grades

<HTML>
  <HEAD>
    <TITLE>Finding Average Grades</TITLE>
  </HEAD>
  <BODY>
    <H1>Finding Average Grades</H1>
    <%
        double grades[] = {8, 9, 7, 6, 5, 4};
        double sum, average;
        sum = 0;
        for (int i = 0; i < grades.length; i++) {
            sum += grades[i];
        }
        average = sum / grades.length;
        out.println("Average grade = " + average);
    %>
  </BODY>
</HTML>





Using the for Statement

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





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>





Your for statement to loop through an array

<HTML>
  <HEAD>
    <TITLE>My First JSP</TITLE>
  </HEAD>
  <BODY>
    <H1>Using the if Statement</H1>
    <%
        int array[] = {1, 2, 3, 4, 5}, sum = 0;
        for (int i = 0; i < array.length; sum += array[i++]);
        out.println("The average = " + sum / array.length);
    %>
  </BODY>
</HTML>