闰年的非法输入的问题

1.问题描述

输入年份,判断其是否为闰年。

2.判断条件

 1)能被4整除且不能被100整除的为闰年。
 2)能被100整除且被400整除的为闰年。

3.等价类划分

编号 有效等价类 编号 无效等价类
1 能被400整除 3 能被100整除不能被4整除
 
2 能被4整除不能被100整除 4 不能被4整除
5 非法输入(包括负数)

4.测试用例

编号 输入 覆盖等价类 输出
1 1600 1 1600是闰年
2 2012 2 2012是闰年
3 1700 3 1700非闰年
4 2014 4 2014非闰年
5 2015 4 2015非闰年
6 .efe32 5 请输入合法年份
7   5 请输入合法年份

 

 

 

 

 

 

5.代码实现

在jsp的function中使用了parseInt()和isNaN()来判断非法输入

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%@ page contentType="text/html; charset=utf-8"%>
 3 <%@ page import="java.sql.*" %>
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8 
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11   <head>
12     <base href="<%=basePath%>">
13    
14     <title>测试</title>
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20   </head>
21  
22   <script>
23   function judge(){
24       var text = document.getElementById(0).value;
25       var i;
26       var str = new Array("是闰年","非闰年","请输入合法年份");
27    
28     var year = parseInt(text);
29       if(year < 0 || isNaN(year)){
30           i = 2;
31       }else if(year % 400 == 0){
32           i = 0;
33       }else if (year % 100 == 0){
34           i = 1;
35       }else if (year % 4 == 0){
36           i = 0;
37      }else i = 1;
38       
39       var out = "";
40       if(i == 2){
41           out += str[i] ;
42       }else{
43           out += text  + str[i];
44       }
45       alert(out);
46   }
47  
48   </script>
49  
50   <body>
51     This is my JSP page. <br>
52     <center><tr style="text-align:center">测试闰年</tr><br><br>
53     <tr style="text-align:center">请输入年份:
54     <input type ="text" id="0"></tr><br><br>
55     <button type="button" onclick="judge();" align ="center">确定</button></tr>
56     </center>
57   </body>
58 </html>

 

6.测试结果

编号 输入 输出
1 1600
2 2012
3 1700
4 2014
5 2015
6 .efe32
7  

 

原文地址:https://www.cnblogs.com/tju-qiran/p/4394517.html