JSP计算闰年个数
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>闰年个数</title>
</head>
<body>
<%
int count = 0;
for (int i = 2000; i <= 2013; i++) {//循环遍历2000-2013年
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
count++;//累加闰年个数
}
}
%>
2000-2013之间一共
<%=count%>
个闰年
</body>
</html>
如需显示对应的闰年年份,代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>闰年个数以及对应的闰年</title>
</head>
<body>
<%!public boolean findYear(int year) {//判断年份是否为闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}%>
<%
int count = 0;
StringBuffer sb = new StringBuffer();
for (int i = 2000; i <= 2013; i++) {//循环遍历2000-2013年
if (findYear(i)) {//调用上面判断闰年的方法进行判断
count++;//累加闰年个数
sb.append(i).append(" ");//添加闰年的年份(为了方便看清,加上空格的转义字符)
}
}
%>
2000-2013之间一共
<%=count%>
个闰年,分别是
<%=sb%>
</body>
</html>