现在的位置: 首页 > 综合 > 正文

if,switch,while,do..while,for语句在JSP中的用法示例

2018年05月15日 ⁄ 综合 ⁄ 共 2113字 ⁄ 字号 评论关闭

if示例

 范例:

  

 

 示例一:

<html>
  <head>
  <title>
   if statement test program
  </title>
  </head>
  <body>
  <%
    int x=15;
    int y=10;
    if(x>y)
    {
      out.print("x=15,y=10"+"<br>");
      out.println("x>y");
     
    }
    else
    {
      out.print("x=15,y=10"+"<br>");
      out.println("x>y");
    }
   %>
  </body>
</html>

 

示例二:

<%@ page contentType="text/html;charset=gb2312" %>
 <html>
   <head>
   <title>
   jsp statement test program
   </title>
   </head>
   <body>
    <%
      int month=3;
      if(month==1||month==2||month==3)
      {
        out.println("春天好!");
      }
      else if(month==4||month==5||month==6)
       {
         out.println("夏天好!");
       }
       else if(month==7||month==8||month==9)
        {
          out.println("秋天好!");
        }
        else
        {
           out.print("冬天好!"); 
        }
     
     %>
   </body>
 </html>

 

switch语句:

 

 

<%@ page contentType="text/html;charset=gb2312" %>
 <html>
  <head>
  <title>
   switch statement test programm
  </title>
  </head>
  <body>
  <%
     int month=9;
     switch(month){
       case 1:
       case 2:
       case 3:
        out.println("春天好!"+"<br>");
        break;
       case 4:
       case 5:
       case 6:
        out.println("夏天好!"+"<br>");
        break;
       case 7:
       case 8:
       case 9:
          out.println("秋天好!"+"<br>");
          break;
       case 10:
       case 11:
       case 12:
          out.print("冬天好!"+"<br>"); 
           break;
         default:
         out.println("this line is always display");
           break;
     } 
   %>
  </body>
 </html>

 

for语句:

 

<%@ page contentType="text/html;charset=gb2312" %>
<html>
  <head>
  <title>
   for statement test program
  </title>
  </head>
  <body>
  <%
    int i,sum=0;
    for(i=1;i<=10;i++)
      sum+=i;
      out.println("1到10的总和为:="+sum);
   %>
  </body>
</html>

 

while语句

 

<%@ page contentType="text/html;charset=gb2312" %>
<html>
  <head>
  <title>
   while statement test program
  </title>
  </head>
  <body>
  <%
    int i=0,sum=0;
     while(i<=10){
      sum+=i;
      i++;
     }
      out.println("1到10的总和为:="+sum);
   %>
  </body>
</html>

 

do..while语句:

 

 

<%@ page contentType="text/html;charset=gb2312" %>
<html>
  <head>
  <title>
   do ...while statement test program
  </title>
  </head>
  <body>
  <%
    int i=0,sum=0;
    do{
       sum+=i;
       i++;
    }while(i<=10);
      out.println("1到10的总和为:="+sum);
   %>
  </body>
</html>

抱歉!评论已关闭.