题目7:编写以下程序:该程序有三个输入变量month、day、year(month、day和year均为整数值,并且满足:1≤month≤12、1≤day≤31和1900≤year≤2050),分别作为输入日期的月份、日、年份,通过程序可以输出该输入日期在日历上的往后一天的日期。
例如,输入为2004年11月30日,则该程序的输出为2004年12月1日。
要求:
(1)提供执行代码、执行方法和程序界面;包括分析过程及测试用例以及测试用例执行结果;(2)按照等价类划分法设计测试用例;
测试结果展示:
超出范围测试:
年超出范围
月超出范围
日超出范围
闰年测试:
跨年测试:
超出边界值测试:
代码如下:
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>黑盒测试</title><style>.main {margin: 0 auto;padding: 20px;width: 500px;border-radius: 20px;box-shadow: 0 6px 6px hsl(0deg 0% 0% / 0.3);background-image: -moz-linear-gradient(135deg, rgb(189, 240, 255), rgb(218, 158, 255));background-image: -webkit-linear-gradient(135deg, rgb(189, 240, 255), rgb(218, 158, 255));background-image: linear-gradient(135deg, rgb(189, 240, 255), rgb(218, 158, 255));}input {width: 100px;height: 20px;border-radius: 5px;}button {width: 180px;height: 40px;border-radius: 8px;border: 0px;box-shadow: 0 6px 6px hsl(0deg 0% 0% / 0.2);font-weight: 600;}.p20 {padding: 20px 0;}</style>
</head><body><div class="main"><div class="p20"><span>请输入年</span><input id="year" type="number" placeholder="请输入年" min="1900" max="2050" value="1900"></div><div class="p20"><span>请输入月</span><input id="month" type="number" placeholder="请输入月" min="1" max="12" value="1"></div><div class="p20"><span>请输入日</span><input id="day" type="number" placeholder="请输入日" min="1" max="31" value="1"></div><div class="p20"><button onclick="change()">点击查看下一天结果</button></div><div class="p20"><span>显示结果:</span><span id="show"></span></div></div><script>function change() {let year = parseInt(document.getElementById("year").value)var month = parseInt(document.getElementById("month").value)var day = parseInt(document.getElementById("day").value)console.log(parseInt(month) === 1)var show = document.getElementById("show")switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:if (day === 31) {month = month + 1;day = 1;} else {day = day + 1;}break;case 4:case 6:case 9:case 11:if (day === 30) {month = month + 1;day = 1;} else {day = day + 1;}break;case 2:if (day === 28) {if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {day = 29;} else {day = 1;month = month + 1;}} else if (day === 29) {if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {day = 1;month = month + 1;} else {console.log("输入的日期不合法");show.innerHTML = "日的值不在指定范围内"}} else if (day < 29) {day = day + 1;} else {console.log("输入的日期不合法");show.innerText = "日的值不在指定范围内"}break;case 12:if (day == 31) {day = 1;month = 1;year = year + 1;} else {day = day + 1;}default:break;}// 这里最后计算为日期,做判断,看是否符合预期if (year >= 1900 && year <= 2050 && month <= 12 && month >= 1 &&day <= 31 && day >= 1) {if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0) {// 是闰年if (month == 2 && day >= 30) {// 在判断是闰年是否大于30天,是就提出日不在指定范围show.innerHTML = "日的值不在指定范围内"console.log("日的值不在指定范围内");} else {show.innerHTML = `下一天是${year}-${month}-${day}`console.log("下一天为" + year + "年" + month + "月" + day + "日");}} else if (month == 2 && day > 28) {show.innerHTML = "日的值不在指定范围内"console.log("日的值不在指定范围内");} else {show.innerHTML = `下一天是${year}-${month}-${day}`console.log("下一天为" + year + "年" + month + "月" + day + "日");}} else if (year < 1900 || year > 2050) {console.log("年的值不在指定范围内");show.innerHTML = "年的值不在指定范围内"} else if (month > 12 || month < 1) {show.innerHTML = "月的值不在指定范围内"console.log("⽉的值不在指定范围内");} else if (day > 31 || day < 1) {show.innerHTML = "日的值不在指定范围内"console.log("日的值不在指定范围内");}}</script>
</body></html>