First, the topic
Determine if the year entered is a leap years
Second, the implementation of the program
I continue to use javascript+html to achieve:
The procedure for judging leap years is as follows, regardless of the input of the exception:
function isleapyear (y) { return (y% 400 = = 0) | | (y% 4 = = 0 && y% = 0 );}
But in practice, you have to consider whether there is an exception input, so I can check the input before calling the function to ensure that the input is valid:
function Isint (input) { var reg =/^[0-9]+$/; if (Reg.test (input)) return true ; return false ;}
Use the Isint () function to ensure that the input is valid before each judgment:
function Check () { var input = document.getElementById ("year"). Value; if (Isint (input)) { if(isleapyear (input)) alert ("is a leap year"); Else alert ("Not a leap year"); } Else alert ("The input is illegal! ");}
Click here to access the program:http://zhaobi.org/softwaretest/Test3.html
Third, testing
Test Case:
Input |
Expected output |
2400 |
Yes |
1900 |
No |
2004 |
Yes |
2015 |
No |
Adc |
Exception |
Null input |
Exception |
Test:
Iv. Source Code
<!DOCTYPE HTML><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>EditBox</title></Head><Body><Divstyle= "width:220px">Please enter the year:<inputtype= "text"ID= "Year"/><BR><inputtype= "button"value= "OK"OnClick= "Check ()"/><Scripttype= "Text/javascript">functionIsint (input) {varReg= /^[0-9]+$/; if(Reg.test (input))return true; return false;}functionisleapyear (y) {return(y% - == 0 ) ||(y% 4 == 0 &&y% - != 0 );}functionCheck () {varinput=document.getElementById (" Year"). Value; if(Isint (input)) {if(Isleapyear (input)) alert ("is a leap year"); ElseAlert ("not a leap year"); } ElseAlert ("The input is illegal! ");}</Script></Div></Body></HTML>
V. Summary
In fact, you can also merge the instrumented input snippet into the Isleapyear () function, which reduces the number of functions.
Software Testing-4 procedures for judging leap years and preventing entry of illegal characters