一些最基础的JSP小程式
1.输出一个由*所构成的金字塔,代码如下:
<%@page contentType="text/html;charset=UTF-8" %>
<%
for(int n=0;n<=5;n++){
for(int m=0;m<n;m++){
out.print("*");
}
out.print("<br>");
}
for(int i=4;i>=0;i--){
for(int j=0;j<i;j++){
out.print("*");
}
out.print("<br>");
}
%>
2.输出一个已知数组的最值
<%@page contentType="text/html;charset=UTF-8" %>
<%
int []a={13,26,35,15,48,26,15,29,37,65,15,14,68,95,32,14,75,96,45,16,47,14,34,36,29,84,75};
int max=0;
int min=999;
for (int i=0;i<a.length-1;i++){
if(a[i]>=max){
max =a[i];
}
if(a[i]<=min){
min =a[i];
}
}
out.print("<tr><td>该数组最大值为"+max);
out.print("<tr><td>该数组最小值为"+min);
%>
3.输出今天是星期几
<%@page contentType="text/html;charset=UTF-8" %>
<body>
<script>
var x
var d=new Date().getDay();
switch (d){
case 0:x="今天是星期日";
break;
case 1:x="今天是星期一";
break;
case 2:x="今天是星期二";
break;
case 3:x="今天是星期三";
break;
case 4:x="今天是星期四";
break;
case 5:x="今天是星期五";
break;
case 6:x="今天是星期六";
break;
}
document.write(x);
</script>
</body>
4.输出几天是星期几2
<html>
<%@page contentType="text/html;charset=UTF-8" %>
<body>
<script type="text/javascript" >
var d=new Date()
var weekday=new Array(7)
weekday[0]="星期日"
weekday[1]="星期一"
weekday[2]="星期二"
weekday[3]="星期三"
weekday[4]="星期四"
weekday[5]="星期五"
weekday[6]="星期六"
document.write("今天是" + weekday[d.getDay()])
</script>
</body>
</html>
5.输出1到10的阶乘的和
<%@page contentType="text/html;charset=UTF-8" %>
<%
int sum=0;
int a=0;
for (int i=1;i<=10;i++){
a=1;
for(int j=1;j<=i;j++){
a=a*j;
out.print(a+"="+(a/j)+"*"+j+"  ");
}
out.print("<br>");
sum=sum+a;
}
out.print("1!+2!+3!+4!+5!+6!+7!+8!+9!+10!的值为"+sum);
%>
6.输出101到200之间有多少个素数
<%@page contentType="text/html;charset=UTF-8" %>
<%
int a=0;
int b=0;
for(int i=101;i<=200;i++){
for(int j=2;j<i;j++){
a=0;
if(i%j==0){
a=a+1;
break;
}
}
if(a==0){
b=b+1;
out.print("<br>"+i);
}
}
out.print(b);
%>
7.输出100到999内水仙花数个数
<%@page contentType="text/html;charset=UTF-8" %>
<%
double a,b,c;
int sum=0;
for(int i=100;i<1000;i++){
a=Math.floor(i/100);
b=Math.floor((i-100*a)/10);
c=i-100*a-10*b;
if((a*a*a+b*b*b+c*c*c)==i){
out.print("<br>"+i);
sum=sum+1;
}
}
out.print(sum);
%>
8.根据输入的参数输出值
<%@page contentType="text/html;charset=UTF-8" %>
<%@include file="/include/gds_include.jsp" %>
<form name="xform" method="post" action="<%=PROGRAM%>">
<input type="text" name="x" >输入x
<input type="text" name="y" >输入y
<input type="submit" value="输出" >
<table border="1" width="100%">
<%
String data1 = request.getParameter("x");
String data2 = request.getParameter("y");
if(data1==null || data2==null){
out.print("x or y is error");
}
else {
out.print("<li>I got a " + data1);
out.print("<li>I got a " + data2);
int trans1 = Integer.parseInt(data2);
int trans2 = Integer.parseInt(data1);
for(int i=1; i<=9; i++) {
out.print("<tr>");
for(int j=1; j<=trans1; j++) {
if(i*j % trans2 == 0) {
out.print("<td><font size=5 font color=red>"+i+"*"+j+"="+(i*j)+"</font>");
}
else {
out.print("<td><font size=5 font color=blue>"+i+"*"+j+"="+(i*j)+"</font>");
}
}
}
}
%>
</table>
9.根据参数输出值
<%@page contentType="text/html;charset=UTF-8"%>
<title>中文网址</title>
<table border="1" width="100%">
<input type="text" name="aa" >输入a
<input type="text" name="bb" >输入b
<%
String data1 = request.getParameter("aa");
String data2 = request.getParameter("bb");
String value = request.getParameter("value");
out.print("<li>I got a " + data1);
out.print("<li>I got a " + data2);
out.print("<li> hello " + value);
try {
int trans1 = Integer.parseInt(data2);
int trans2 = Integer.parseInt(data1);
for(int i=1; i<=9; i++) {
out.print("<tr>");
for(int j=1; j<=trans1; )
do{{
do{
out.print("<td align=\"center\"><font size=5 font color=\"red\">"+i+"*"+j+"="+(i*j)+"完成"+"</font>");
}
while(i*j % trans2 == 0) ;
out.print("<td align=\"right\"><font size=5 font color=\"blue\">"+i+"*"+j+"="+(i*j)+"完成"+"</font>");
j=j+1;
}
}
while(j<trans1);
}
}